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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8341885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:07:30  来源:igfitidea点击:

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

javascriptxml

提问by Tokendra Kumar Sahu

I am generating XML using Javascript. It works fine if there are no special characters in the XML. Otherwise, it will generate this message: "invalid xml".

我正在使用 Javascript 生成 XML。如果 XML 中没有特殊字符,它就可以正常工作。否则,它将生成此消息:“无效的 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.

谢谢。

采纳答案by maerics

Consider generating the XML using DOM methods. For example:

考虑使用 DOM 方法生成 XML。例如:

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 实体转义问题,但可能存在一些跨浏览器问题。

回答by jabclab

This will do the replacement in JavaScript:

这将在 JavaScript 中进行替换:

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

This uses regular expression literalsto replace all less than and greater than symbols with their escaped equivalent.

这使用正则表达式文字将所有小于和大于符号替换为其转义等价物。

回答by Federico Zancan

JavaScript comes with a powerful replace()method for string objects.

JavaScriptreplace()为字符串对象提供了一个强大的方法。

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.

.replace()method的第一个参数是您希望替换的原始字符串部分。它可以由纯字符串对象(甚至文字)或正则表达式表示。

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()此处找到完整的参考资料。