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?
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
valueonlyConversely 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
_.escapemethod to perform this functionality.