javascriptstringxml-entities

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 xml entities where needed. Something like htmlentities in PHP but for XML and in Javascript.

Thank you for any help!


Solution

  • 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:

    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.