0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.