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?
if something is escaped from before, you could try this since this will not double escape like many others
HTML encoding is simply replacing
&
,"
,'
,<
and>
chars with their entity equivalents. Order matters, if you don't replace the&
chars first, you'll double encode some of the entities:As @Johan B.W. de Vries pointed out, this will have issues with the tag names, I would like to clarify that I made the assumption that this was being used for the
value
onlyConversely if you want to decode HTML entities1, make sure you decode
&
to&
after everything else so that you don't double decode any entities:1 just the basics, not including
©
to©
or other such thingsAs far as libraries are concerned. Underscore.js (or Lodash if you prefer) provides an
_.escape
method to perform this functionality.This might be a bit more efficient with the same outcome:
This is simple:
If you have jQuery, here's a simple solution:
Use it like this:
"<foo&bar>".htmlEscape();
->"<foo&bar>"
Technically, &, < and > aren't valid XML entity name characters. If you can't trust the key variable, you should filter them out.
If you want them escaped as HTML entities, you could use something like http://www.strictly-software.com/htmlencode .