Git Product home page Git Product logo

node-js2xmlparser's Introduction

js2xmlparser

Build Status 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 new 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 gulp:

git clone https://github.com/michaelkourlas/node-js2xmlparser.git
npm install
gulp

You'll need to install gulp first if you don't have it:

npm install -g gulp

You can then copy the folder into your node_modules directory.

The default target 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 target prod. You can also build the development version using the target dev. At the moment, 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 gulp:

gulp docs

Examples

The following example illustrates the basic usage of js2xmlparser:

var js2xmlparser = require("js2xmlparser");

var obj = {
    "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": "[email protected]"
};

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

This example produces the following XML:

<?xml version='1.0'?>
<person>
    <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>[email protected]</email>
</person>

Additional examples can be found in examples/example.js.

Tests

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

gulp test

The test target builds the production variant of js2xmlparser before running the tests. The test-prod target does the same thing, while the test-dev target builds the development variant first instead.

License

js2xmlparser is licensed under the Apache License 2.0. Please see the LICENSE.md file for more information.

node-js2xmlparser's People

Contributors

apipkin avatar bschaepper avatar danieljuhl avatar gingi avatar manumaticx avatar mharward avatar michaelkourlas avatar rmg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.