Skip to main content
Rocko
19-Tanzanite
November 10, 2021
Solved

SOAP Webservices & parsing XML & JSON

  • November 10, 2021
  • 3 replies
  • 3469 views

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.

Best answer by 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.

3 replies

14-Alexandrite
November 10, 2021

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

19-Tanzanite
November 11, 2021

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!

14-Alexandrite
November 11, 2021

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

Rocko
Rocko19-TanzaniteAuthorAnswer
19-Tanzanite
September 9, 2022

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.