Reputation: 6305
I have got a xml file that might get the 5 special characters like "&" inserted during a process.I know xml will throw error while parsing these characters.Im using the jQuery ajax method to get the data from the xml file.Is there any way of overcoming this parser error in javascript like replacing the special characters?
Upvotes: 1
Views: 16125
Reputation: 2823
XMLText = XMLText.Replace("&", "&");
XMLText = XMLText.Replace("""", """);
XMLText = XMLText.Replace("'", "'");
XMLText = XMLText.Replace("<", "<");
XMLText = XMLText.Replace(">", ">");
return XMLText;
Upvotes: 0
Reputation: 5858
Let's say a user inputs this:
<foo> >bar< hi & bye >/bar<
Now, they likely meant:
<foo> >bar< hi & bye >/bar< </foo>
But they could have meant:
<foo> &gt;bar&lt; hi & bye &gt;/bar&lt; </foo>
You can clean up and sanitize text that should be well formed XML by closing unclosed tags and escaping &s. I'm not familiar with any javascript library that will do this for you.
Upvotes: 1
Reputation: 14843
In short, yes, assuming you get your xml as a string (which if I recall correctly, jQuery does). Then you can do the following:
xmlstr = xmlstr.replace(/&/g, "&");
for any characters you want to replace.
Upvotes: 2