简体   繁体   中英

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

您需要使用某些软件来转义xml,以将其嵌入到json,然后转义json,以将其嵌入到xml。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM