Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
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.
Solved! Go to Solution.
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.
This is great, Rocko! Thank you for sharing! Will definitely go in my toolbox.
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.
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.