cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

SOAP Webservices & parsing XML & JSON

Rocko
18-Opal

SOAP Webservices & parsing XML & JSON

Sometimes you have to work with XML in Thingworx, e.g. when processing the result of a SOAP Webservice call.

This can be done using E4X, but it's not comfortable to use, especially when there are namespaces involved.

 

Therefore, I took a publicly available (GPLed) script which converts the whole reply to JSON and adapted it to Thingworx. It creates an object from the XML and returns the JSON.stringify representation of it. You can take it and extend it to  your needs. If you know what you need from the reply and E4X, using E4X directly is probably faster, as it does need the intermediate object and JSON creation, but this is much more convenient (you can work with either the object or the JSON).

 

Beware this only works with valid XML, so an error message returned from a failed WS call, which could be HTML will not be parsed.

The thing was exported from TWX 9.2 but should work with older versions as well.

ACCEPTED SOLUTION

Accepted Solutions
Rocko
18-Opal
(To:Rocko)

I updated this example for the case where there is XML Mixed Content like <a>content<subtag>othercontent</subtag></a>.

The original version swallowed the text "content", the newer version includes it to the JSON output.

View solution in original post

4 REPLIES 4
eliotlandrum
14-Alexandrite
(To:Rocko)

This is great, Rocko! Thank you for sharing! Will definitely go in my toolbox.

VladimirRosu
19-Tanzanite
(To:Rocko)

I opened to see if this is an article asking for help in parsing XML, and guess what I see. Time to put aside my deep E4X example then

Thank you for sharing this with the community Rocko!

Ooooh... I've got some examples of E4X over in the tips forum, but always enjoy some more.

Rocko
18-Opal
(To:Rocko)

I updated this example for the case where there is XML Mixed Content like <a>content<subtag>othercontent</subtag></a>.

The original version swallowed the text "content", the newer version includes it to the JSON output.

Announcements


Top Tags