I tried to replace some special characters, like:
xmlData=xmlData.replaceAll(">",">"); xmlData=xmlData.replaceAll("&","&"); //but it doesn't work.
<category label='ARR Builders & Developers'>
Consider generating the XML using DOM methods. For example:
var c = document.createElement("category"); c.setAttribute("label", "ARR Builders & Developers"); var s = new XMLSerializer().serializeToString(c); s; // => "<category label=\"ARR Builder & Developers\"></category>"
This strategy should avoid the XML entity escaping problems you mention but might have some cross-browser issues.
xml = xml.replace(/</g, "<"); xml = xml.replace(/>/g, ">");
This uses regular expression literals to replace all less than and greater than symbols with their escaped equivalent.
replace() method for string objects.
In general – and basic – terms, it works this way:
var myString = yourString.replace([regular expression or simple string], [replacement string]);
The first argument to
.replace() method is the portion of the original string that you wish to replace. It can be represented by either a plain string object (even literal) or a regular expression.
The regular expression is obviously the most powerful way to select a substring.
The second argument is the string object (even literal) that you want to provide as a replacement.
In your case, the replacement operation should look as follows:
xmlData=xmlData.replace(/&/g,"&"); xmlData=xmlData.replace(/>/g,">"); //this time it should work.
Notice the first replacement operation is the ampersand, as if you should try to replace it later you would screw up pre-existing well-quoted entities for sure, just as
In addition, pay attention to the regex ‘g’ flag, as with it the replacement will take place all throughout your text, not only on the first match.
I used regular expressions, but for simple replacements like these also plain strings would be a perfect fit.
You can find a complete reference for