Home Ask Login Register

Developers Planet

Your answer is one click away!

Bradley February 2016

Parsing XML with JSON, also containing XML, embedded in an attribute of a tag

I have an XML response, similar to below, which I need to parse the <message> out of which is contained within some JSON inside of an attribute. It comes in the form of a string.

"<Response attr1='hello' bar='{ \"int\": 50, \"bool\": false, \"message\":\"&lt; message &gt; Test Message &lt;a href=\"http://www.blah.com\"&gt;here&lt;/a&gt;  &lt; /message &gt;\" }'/>;"

I've found some nice pure Javascript answers such as here which was perfect however, as you can see from the example above, the XML now contains JSON with XML within, and this parser also parses the <a> and <message> tags which I need to preserve.

What is the easiest way of going about implementing this? My solution needs to be pure Javascript, no libraries.

EDIT: https://jsfiddle.net/ree3c7w0/ shows my attempt at trying to extract the JSON whilst preserving the XML within the JSON and the only way to do this is to escape the XML within the JSON


Gavriel February 2016

You need to use some software to escape the xml for embedding it to the json and then escape the json for embedding it to the xml.

Post Status

Asked in February 2016
Viewed 1,248 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life