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!
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, '<').replace(/>/g, '>')
.replace(/\t/g, '	').replace(/\n/g, '
').replace(/\r/g, '
')
);
}
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 '
instead of '
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.