How to handle special characters in URLs inside XML

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

When analyzing this question, I have two problems:
1.) The character ( & ) breaks the entire syntax, if not replaced, and amp (which violates the URL)
2.) The comma ( , ) tries to send my parser to the next child, which will lead to incomplete Url.

What can I do to fix this?
I am using Javascript and PHP.

+4
source share
2 answers

Replacing &with &should not violate the URL. You did not specify :?

The best solution is you should wrap this in a CDATA tag:

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

Which tells the XML parser to treat it as text, rather than parse &.

+12
source

There are certain characters in XML that are not allowed - you need to β€œescape” them in the XML document.

These characters and their "escaped" versions:

>  &gt;
<  &lt;
&  &amp;
'   &apos;
"   &quot;
+1
source

All Articles