js2xmlparser is a Node.js module that parses JavaScript objects into XML.
Features
Since XML is a data-interchange format, js2xmlparser is designed primarily for
JSON-type objects, arrays and primitive data types, like many of the other
JavaScript to XML parsers currently available for Node.js.
However, js2xmlparser is capable of parsing any object, including native
JavaScript objects such as Date and RegExp, by taking advantage of each
object's toString function or, if this function does not exist, the String
constructor.
js2xmlparser also has support for the Map and Set objects introduced in
ECMAScript 2015, treating them as JSON-type objects and arrays respectively.
Support for Maps is necessary to generate XML with elements in a specific
order, since JSON-type objects do not guarantee insertion order. Map keys are
always converted to strings using the method described above.
js2xmlparser also supports a number of constructs unique to XML:
attributes (through an attribute property in objects)
mixed content (through value properties in objects)
multiple elements with the same name (through arrays)
js2xmlparser can also pretty-print the XML it outputs.
Installation
The easiest way to install js2xmlparser is using npm:
npm install js2xmlparser
You can also build js2xmlparser from source using npm:
The build script will build the production variant of js2xmlparser, run all
tests, and build the documentation.
You can build the production variant without running tests using the script
prod. You can also build the development version using the script dev.
The only difference between the two is that the development version includes
source maps.
Usage
The documentation for the current version is available here.
You can also build the documentation using npm:
npm run-script docs
Examples
The following example illustrates the basic usage of js2xmlparser: