How to deal with special characters in URLs inside XML

牧云@^-^@ 提交于 2020-01-03 15:35:19

问题


I have an XML element that has a url as one of it's children, for example:
http://maps.google.com/FortWorth&Texas,more+url;data

When parsing this, I'm having two issues:
1.) The (&) symbol breaks the entire parse unless replaced with &amp (which breaks the url)
2.) The comma (,) tries to send my parser on to the next child, resulting in an incomplete url.

What can I do to remedy this?
I'm using Javascript and PHP.


回答1:


Replacing & with & shouldn't break the url. Did you left out the ;?

Better solution is you should wrap that in a CDATA tag:

<![CDATA[ http://maps.google.com/FortWorth&Texas,more+url;data ]]>

Which tells the XML parser to treat it as text and not parse the &.




回答2:


There are certain characters which are not valid in XML - you need to "escape" these in the xml document.

These characters and their "escaped" versions are:

>  &gt;
<  &lt;
&  &amp;
'   &apos;
"   &quot;


来源:https://stackoverflow.com/questions/4341145/how-to-deal-with-special-characters-in-urls-inside-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!