简体   繁体   English

如何使用JavaScript在XML文件中添加特殊字符,如&>

[英]How to add special characters like & > in XML file using JavaScript

I am generating XML using Javascript. 我正在使用Javascript生成XML。 It works fine if there are no special characters in the XML. 如果XML中没有特殊字符,它可以正常工作。 Otherwise, it will generate this message: "invalid xml". 否则,它将生成此消息:“invalid xml”。

I tried to replace some special characters, like: 我试图替换一些特殊字符,例如:

xmlData=xmlData.replaceAll(">",">");
xmlData=xmlData.replaceAll("&","&");
//but it doesn't work.

For example: 例如:

<category label='ARR Builders & Developers'>

Thanks. 谢谢。

Consider generating the XML using DOM methods . 考虑使用DOM方法生成XML For example: 例如:

var c = document.createElement("category");
c.setAttribute("label", "ARR Builders & Developers");
var s = new XMLSerializer().serializeToString(c);
s; // => "<category label=\"ARR Builder &amp; Developers\"></category>"

This strategy should avoid the XML entity escaping problems you mention but might have some cross-browser issues. 此策略应该避免XML实体转移您提到的问题但可能存在一些跨浏览器问题。

This will do the replacement in JavaScript: 这将在JavaScript中进行替换:

xml = xml.replace(/</g, "&lt;");
xml = xml.replace(/>/g, "&gt;");

This uses regular expression literals to replace all less than and greater than symbols with their escaped equivalent. 这使用正则表达式文字来替换所有小于和大于符号的转义等价物。

JavaScript comes with a powerful replace() method for string objects. JavaScript为字符串对象提供了强大的replace()方法。

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. .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,"&amp;");
xmlData=xmlData.replace(/>/g,"&gt;");
//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 "&amp;gt;" 请注意,第一个替换操作是&符号,就好像您应该尝试更换它,以后您可能会将预先存在的引用良好的实体搞砸,就像"&amp;gt;" .

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. 此外,请注意正则表达式'g'标志,因为替换将在整个文本中进行,而不仅仅是在第一场比赛中。

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 String.replace() here . 您可以在此处找到String.replace()的完整参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM