开发者

How to make a valid string for XML in JavaScript?

I'm looking for a function to convert a string to the xml string with xm开发者_如何学运维l entities where needed. Something like htmlentities in PHP but for XML and in Javascript.

Thank you for any help!


There's nothing built-in (except innerHTML serialisation which is super-dodgy for this purpose), you'd have to write it yourself, eg.:

function encodeXml(s) {
    return (s
        .replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''')
        .replace(/</g, '&lt;').replace(/>/g, '&gt;')
        .replace(/\t/g, '&#x9;').replace(/\n/g, '&#xA;').replace(/\r/g, '&#xD;')
    );
}

This is a maximalist escaping function for safety:

  • it will always encode ", ' and tab/CR/LF characters though they only need to be escaped in an attribute value, where that particular quote character is being used as a delimiter.

  • it will always encode > though this only actually needs to be escaped when part of the ]]> sequence in text content.

If you don't need these properties you can remove the replace​s you don't need (it's pretty rare to need to put tab/CR/LF in an attribute value, for example).

If you need to produce HTML-compatible XHTML, use &#39; instead of &apos; if you need that escape.

In general you should avoid htmlentities and use htmlspecialchars instead, as htmlentities unnecessarily encodes all non-ASCII characters as HTML entity references, which also has the side-effect of screwing up your text if you don't give it the right $charset parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜