XML Parsing error not well formed. The problem is

2019-05-21 19:13发布

问题:

Working on an API but I've run into a problem.

This specific part of the response (in XML) returns a URL. Here's the error I'm getting:

XML Parsing Error: not well-formed

<item_to_page_url>http://cnn.com/.../?hpt=C2&replytocom=11119#respond</item_to_page_url>

First off, I added an ellipses to shorten the length of that for viewing purposes here on the forum. Now, The error tracker line thing, that I didn't include above, points to the second equals = character in that URL.

So. I can't help that this URL is showing up in the XML response. Is there some sort of workaround? Surely there's some way to tell XML that it's CONTENT, not <tags> and make it ignore this..

Thanks

--EDIT for comment below --

<item_to_page_title><![CDATA[Breaking news, real-time scores and daily analysis from Sports Illustrated  SI.com]]></item_to_page_title>

same error as before, different label, different character.

回答1:

Surely there's some way to tell XML that it's CONTENT, not and make it ignore this..

There is indeed. CDATA tags will make the XML parser ignore content contained within them.

<item_to_page_url><![CDATA[http://cnn.com/.../?hpt=C2&replytocom=11119#respond]]></item_to_page_url>

However, if you don't want the parser to completely ignore the content, you can just escape it. The error you're receiving is actually not caused by the equals sign, it is caused by the amperstand before it - amperstands must be escaped in xml as &amp;

<item_to_page_url>http://cnn.com/.../?hpt=C2&amp;replytocom=11119#respond</item_to_page_url>