xml2js Cheat Sheet

Ever had the urge to parse XML? And wanted to access the data in some sane, easy way? Don't want to compile a C parser, for whatever reason? Then xml2js is what you're looking for!


npm install xml2js
bower install xml2js


XML to Json

var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {

Json to XML

var xml2js = require('xml2js');

var obj = {name: "Super", Surname: "Man", age: 23};

var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);


var xml2js = require('xml2js');
var xml = '<foo></foo>';

var parser = new xml2js.Parser(/* options */);
parser.parseStringPromise(xml).then(function (result) {
.catch(function (err) {
  // Failed



attrkeyPrefix that is used to access the attributes. Version 0.1 default was @.
charkeyPrefix that is used to access the character content. Version 0.1 default was #.
explicitCharkeyDetermines whether or not to use a charkey prefix for elements with no attributes.
trimTrim the whitespace at the beginning and end of text nodes.
normalizeTagsNormalize all tag names to lowercase.
normalizeTrim whitespaces inside text nodes.
explicitRootSet this if you want to get the root node in the resulting object.
emptyTagwhat will the value of empty nodes be.
explicitArrayAlways put child nodes in an array if true;
ignoreAttrsIgnore all XML attributes and only create text nodes.
mergeAttrsMerge attributes and child elements as properties of the parent, instead of keying attributes off a child attribute object.
validatorYou can specify a callable that validates the resulting structure somehow, however you want. See unit tests for an example.
xmlnsGive each element a field usually called '$ns' (the first character is the same as attrkey) that contains its local name and namespace URI.
explicitChildrenPut child elements to separate property. Doesn't work with mergeAttrs = true.
childkeyPrefix that is used to access child elements if explicitChildren is set to true.
preserveChildrenOrderModifies the behavior of explicitChildren so that the value of the "children" property becomes an ordered array.
charsAsChildrenDetermines whether chars should be considered children if explicitChildren is on.
includeWhiteCharsDetermines whether whitespace-only text nodes should be included.
asyncShould the callbacks be async.
strictSet sax-js to strict or non-strict parsing mode.
attrNameProcessorsAllows the addition of attribute name processing functions.
attrValueProcessorsAllows the addition of attribute value processing functions.
tagNameProcessorsAllows the addition of tag name processing functions.
valueProcessorsAllows the addition of element value processing functions.


attrkeyPrefix that is used to access the attributes.
charkeyPrefix that is used to access the character content.
rootName(default root or the root key name): root element name to be used in case explicitRoot is false or to override the root element name.
renderOptsRendering options for xmlbuilder-js.
prettyRendering options for xmlbuilder-js.
indentwhitespace for indentation (only when pretty)
newlinenewline char (only when pretty)
xmldecXML declaration attributes.
xmldec.versionA version number string, e.g. 1.0
XML doc.encodingEncoding declaration, e.g. UTF-8
xmldec.standalonestandalone document declaration: true or false
doctypeoptional DTD. Eg. {'ext': 'hello.dtd'}
headlessomit the XML header.
allowSurrogateCharsAllows using characters from the Unicode surrogate blocks.
cdatawrap text nodes in <![CDATA[ ... ]]> instead of escaping when necessary. Does not add <![CDATA[ ... ]]> if it is not required.