JavaScript to XML Parser

5.0.0 · active · verified Sun Apr 19

js2xmlparser is a Node.js module that converts JavaScript objects into XML. It is currently in its stable version 5.0.0 and has a consistent release cadence with significant updates in major versions. The library specializes in parsing JSON-type objects, arrays, and primitive data types, but also supports native JavaScript objects like `Date` and `RegExp` by leveraging their `toString` methods. A key differentiator is its explicit support for ES2015 `Map` and `Set` objects, enabling ordered XML element generation where standard JSON objects would not guarantee it. It also features robust handling of XML-specific constructs such as attributes (using an `@` property), mixed content (using a `#` property), and multiple elements with the same name (through arrays), alongside pretty-printing capabilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a complex JavaScript object into XML, showcasing attribute handling, nested objects, array elements, and mixed content using the `parse` function.

import { parse } from 'js2xmlparser';

const 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"
};

const xmlOutput = parse("person", obj);
console.log(xmlOutput);

view raw JSON →