xml2js
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
sax | 1.3.0 | 14.99 kB | ISC | prod | |
xmlbuilder | 9.0.7 | 18.57 kB | MIT | prod |
xml2js is a simple XML to JavaScript object converter that allows you to parse XML seamlessly. You do not need a compiled C parser, making xml2js an easy, accessible solution for handling XML data. This tool utilizes sax-js and xmlbuilder-js and supports bi-directional conversion. Note that xml2js is not a full DOM parser, so if your project requires this, you might want to consider JSDom.
You can quickly install xml2js using npm or Bower with the command npm install xml2js
or bower install xml2js
, respectively. Then, using xml2js is straightforward. Here are some usage examples:
For basic, "shoot-and-forget" usage:
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
If you need to use special options:
parseString(xml, {trim: true}, function (err, result) {
});
There's also a simpler usage pattern, requiring no event listeners:
var fs = require('fs'),
xml2js = require('xml2js');
var parser = new xml2js.Parser();
fs.readFile(__dirname + '/foo.xml', function(err, data) {
parser.parseString(data, function (err, result) {
console.dir(result);
console.log('Done');
});
});
For more complex usage, parsing multiple files, and additional features, please refer to the documentation.
xml2js detailed documentation with various use cases and code examples can be found in the repository's README file on GitHub: https://github.com/Leonidas-from-XIV/node-xml2js. The README provides extensive information on installation, usage, options, and updates to new versions, among other data. Always refer to the README for the most up-to-date documentation.