In JavaScript (server side nodejs) I'm writing a program which generates xml as output.
I am building the xml by concatenating a string:
str += '<' + key + '>';
str += value;
str += '</' + key + '>';
The problem is: What if value
contains characters like '&'
, '>'
or '<'
?
What's the best way to escape those characters?
or is there any javascript library around which can escape XML entities?
you can use the below method. I have added this in prototype for easier access. I have also used negative look-ahead so it wont mess things, if you call the method twice or more.
Usage:
Decoding is automaticaly handeled in XML parser.
Method :
I originally used the accepted answer in production code and found that it was actually really slow when used heavily. Here is a much faster solution (runs at over twice the speed):
maybe you can try this,
reference