npm install xml2js
bower install xml2js
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
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
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) {
console.dir(result);
})
.catch(function (err) {
// Failed
});
attrkey | Prefix that is used to access the attributes. Version 0.1 default was @. |
charkey | Prefix that is used to access the character content. Version 0.1 default was #. |
explicitCharkey | Determines whether or not to use a charkey prefix for elements with no attributes. |
trim | Trim the whitespace at the beginning and end of text nodes. |
normalizeTags | Normalize all tag names to lowercase. |
normalize | Trim whitespaces inside text nodes. |
explicitRoot | Set this if you want to get the root node in the resulting object. |
emptyTag | what will the value of empty nodes be. |
explicitArray | Always put child nodes in an array if true; |
ignoreAttrs | Ignore all XML attributes and only create text nodes. |
mergeAttrs | Merge attributes and child elements as properties of the parent, instead of keying attributes off a child attribute object. |
validator | You can specify a callable that validates the resulting structure somehow, however you want. See unit tests for an example. |
xmlns | Give each element a field usually called '$ns' (the first character is the same as attrkey) that contains its local name and namespace URI. |
explicitChildren | Put child elements to separate property. Doesn't work with mergeAttrs = true. |
childkey | Prefix that is used to access child elements if explicitChildren is set to true. |
preserveChildrenOrder | Modifies the behavior of explicitChildren so that the value of the "children" property becomes an ordered array. |
charsAsChildren | Determines whether chars should be considered children if explicitChildren is on. |
includeWhiteChars | Determines whether whitespace-only text nodes should be included. |
async | Should the callbacks be async. |
strict | Set sax-js to strict or non-strict parsing mode. |
attrNameProcessors | Allows the addition of attribute name processing functions. |
attrValueProcessors | Allows the addition of attribute value processing functions. |
tagNameProcessors | Allows the addition of tag name processing functions. |
valueProcessors | Allows the addition of element value processing functions. |
attrkey | Prefix that is used to access the attributes. |
charkey | Prefix 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. |
renderOpts | Rendering options for xmlbuilder-js. |
pretty | Rendering options for xmlbuilder-js. |
indent | whitespace for indentation (only when pretty) |
newline | newline char (only when pretty) |
xmldec | XML declaration attributes. |
xmldec.version | A version number string, e.g. 1.0 |
XML doc.encoding | Encoding declaration, e.g. UTF-8 |
xmldec.standalone | standalone document declaration: true or false |
doctype | optional DTD. Eg. {'ext': 'hello.dtd'} |
headless | omit the XML header. |
allowSurrogateChars | Allows using characters from the Unicode surrogate blocks. |
cdata | wrap text nodes in <![CDATA[ ... ]]> instead of escaping when necessary. Does not add <![CDATA[ ... ]]> if it is not required. |