Skip to content

Some content lost while parsing string to xml

I have got some HTML code through AJAX responseText, which is 350-400 lines long. It has been stored in the variable text as string and I would like to parse it to XML. However, some of the content is removed while parsing. The result ends in the script part and none of the body part of the code can be seen.

var xml = (new DOMParser()).parseFromString(text, "text/xml");
console.log((new XMLSerializer()).serializeToString(xml));

For the result of console.log(text), it returns the full code.

But The result of console.log((new XMLSerializer()).serializeToString(xml)) returns a very incomplete one, which ends like that:

for (i = 0; i </script></head></html>

And the alert one shows null, so I’m pretty sure that the content has lost since the parsing process.

Can anyone tell me how to solve it?



HTML isn’t always valid XML. From your partial output it appears that you are trying to parse HTML. In that case, you should change the second parameter of parseFromString to "text/html" instead of "text/xml". After this the XML serialization should work fine.

Additionally, to get the serialized string you can also access the property documentElement.outerHTML on Document object returned from parseFromString method.

User contributions licensed under: CC BY-SA
5 People found this is helpful