Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

js2xmlparser

michaelkourlas8.1mApache-2.05.0.0TypeScript support: included

Parses JavaScript objects into XML

convert, converter, javascript, js, json, object, objects, parse, parser, xml

readme

js2xmlparser

Node.js CI npm version

Overview

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:

git clone https://github.com/michaelkourlas/node-js2xmlparser.git
npm install
npm run-script build

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:

var js2xmlparser = require("js2xmlparser");

var obj = {
    "@": {
        type: "natural",
    },
    firstName: "John",
    lastName: "Smith",
    dateOfBirth: new Date(1964, 7, 26),
    address: {
        "@": {
            type: "home",
        },
        streetAddress: "3212 22nd St",
        city: "Chicago",
        state: "Illinois",
        zip: 10000,
    },
    phone: [
        {
            "@": {
                type: "home",
            },
            "#": "123-555-4567",
        },
        {
            "@": {
                type: "cell",
            },
            "#": "890-555-1234",
        },
        {
            "@": {
                type: "work",
            },
            "#": "567-555-8901",
        },
    ],
    email: "john@smith.com",
};

console.log(js2xmlparser.parse("person", obj));

This example produces the following XML:

<?xml version='1.0'?>
<person type='natural'>
    <firstName>John</firstName>
    <lastName>Smith</lastName>
    <dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Summer Time)</dateOfBirth>
    <address type='home'>
        <streetAddress>3212 22nd St</streetAddress>
        <city>Chicago</city>
        <state>Illinois</state>
        <zip>10000</zip>
    </address>
    <phone type='home'>123-555-4567</phone>
    <phone type='cell'>890-555-1234</phone>
    <phone type='work'>567-555-8901</phone>
    <email>john@smith.com</email>
</person>

Additional examples can be found in the examples directory.

Tests

js2xmlparser includes a set of tests to verify core functionality. You can run the tests using npm:

npm run-script test-prod

The only difference between the test-prod and test-dev scripts is that the development version includes source maps.

License

js2xmlparser is licensed under the Apache License 2.0.

changelog

5.0.0

  • Allow typeHandlers to be used with bare text and attribute values
  • Clarify that typeHandlers cannot be used with aliases in documentation

4.0.2

  • Update dependencies
  • Export options interfaces in main module
  • Update example to include root attribute

4.0.1

  • Update dependencies
  • Use ESLint instead of TSLint
  • Use npm instead of gulp

4.0.0

  • Do not indent multi-line strings
  • Use self-closing tags, unless otherwise specified
  • Add option to automatically replace invalid characters with U+FFFD
  • Add option to suppress certain values from output
  • Add support for adding to existing xmlcreate object
  • Remove certain unnecessary validation rules
  • Bug fixes
  • Correct errors in documentation

3.0.0

  • Bug fixes
  • Add null and undefined in type declarations
  • Remove explicit engines requirement

2.0.2

  • Bug fixes

2.0.1

  • Remove unnecessary development dependencies from npm shrinkwrap

2.0.0

  • Re-write in TypeScript
  • Re-write to use xmlcreate (greatly simplifies module source)
  • Added support for the ECMAScript 2015 Map and Set objects
  • New method of calling module:

    var js2xmlparser = require("js2xmlparser");
    
    var root = "root";
    var data = {hello: "world"};
    var options = {};
    
    // old method (no longer works):
    // js2xmlparser(root, data, options);
    
    // new method:
    js2xmlparser.parse(root, data, options);
  • New options and changes to functionality of some existing options:

    • declaration contains additional options
    • attributeString has additional functionality
    • valueString has additional functionality
    • The functionality provided by prettyPrinting is now provided by the new format option, which contains additional options
    • arrayMap is now wrapHandlers to reflect the fact that wrapping is provided for both arrays and ES2015 sets
    • convertMap is now typeHandlers to match the name change to arrayMap
    • The functionality provided by useCDATA is now provided by the new cdataInvalidChars and cdataKeys options, which also provide additional functionality
    • Added support for document type definitions using the dtd option

1.0.0

  • First stable release
  • Add arrayMap feature
  • Switch to semantic versioning
  • Switch to Apache 2.0 license

0.1.9

  • Fix error in example.js

0.1.8

  • Reconcile readme and tests with examples

0.1.7

  • Added .gitattributes to .gitignore file
  • Minor tweaks to examples

0.1.6

  • Addition of alias string option
  • Minor changes to examples
  • Minor fixes to tests

0.1.5

  • Bug fixes
  • Minor changes to examples

0.1.4

  • Removed callFunctions option (functionality already provided by convertMap option)
  • Removed wrapArray option (functionality already provided by existing array functionality)
  • Escape numbers when at tbe beginning of an element name
  • Edits to documentation
  • Added tests
  • Added copyright headers to individual JS files

0.1.3

  • Fixed crash when undefined objects are converted to strings
  • Added callFunctions option
  • Added wrapArray option
  • Added useCDATA option
  • Added convertMap option
  • Added copyright year and "and other contributors" to license

0.1.2

  • Fixed crash when null objects are converted to strings

0.1.1

  • Fixed accidental truncation of XML when pretty-printing is disabled
  • Removed copyright year from license

0.1.0

  • Initial release