0

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

Community
  • 1
  • 1
Bradley
  • 1,234
  • 1
  • 12
  • 24
  • XML inside JSON was escaped, but JSON inside XML wasn't. You need to encode JSON before setting it in the XML. – gurvinder372 Feb 08 '16 at 11:46
  • @gurvinder372 Do you mean that the XML inside the JSON should be escaped so it isn't parsed when it shouldn't be? – Bradley Feb 08 '16 at 11:50
  • I mean that just XML inside JSON is parsed so that JSON parsing shouldn't fail, similarly JSON inside XML should be decoded so that XML parsing doesn't fail. – gurvinder372 Feb 08 '16 at 11:54
  • this xml is invalid, check on http://www.xmlvalidation.com/ – Gavriel Feb 08 '16 at 12:08
  • @gurvinder372 So if you were to receive this piece of XML as a response, and you needed to parse both the attributes of the response, and also the JSON within one of the attributes, what would be your approach? It seems like from your answer that I should be expecting more correct XML – Bradley Feb 08 '16 at 12:08
  • what is your question? your solution works, when your input is correctly escaped – Gavriel Feb 08 '16 at 12:14
  • @Gavriel I suppose now that I know the response needs to be escaped, I just need to figure out how to escape the XML correctly – Bradley Feb 08 '16 at 12:17
  • This XML itself is wrong formed. `bar="{ "int"` isn't correct – gurvinder372 Feb 08 '16 at 12:17
  • @gurvinder372 Ah yes, I've updated with my attempt at escaping, though still comes up invalid – Bradley Feb 08 '16 at 12:19
  • I also have to put spaces between the tag name and the escaped tags because the XML parser also parses those tags, which is what I'm trying to avoid – Bradley Feb 08 '16 at 12:27
  • @Bradley what language do you use in your server side? – Gavriel Feb 08 '16 at 12:32

1 Answers1

0

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.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • I escaped the XML within the JSON, and then escaped the JSON within the attribute and then ran it through the parser to try and get the message tag still and they were removed in the parsing process, same issue! – Bradley Feb 08 '16 at 13:51
  • what was removed? the escaping? that's ok. – Gavriel Feb 08 '16 at 13:53
  • I've realised what was being removed isn't important. Is it standard practice for XML like this to be escaped? As I can imagine working with embedded XML/JSON is a nightmare when it's not! – Bradley Feb 08 '16 at 13:55
  • 1
    Read this http://kunststube.net/escapism/ to get the idea of why escaping is needed, and how it is done. About the nightmare: that is a nightmare, that's why I suggested you should not do it manually but by using a function by the language you use on your server – Gavriel Feb 08 '16 at 13:57