National Palace Museum
Learn how companies like Phone2Action are using npm in their front-end developmentJoin the conversation »

obj-to-xml

1.0.5 • Public • Published

obj-to-xml

Converts an object to an XML

Purpose

This package serves in REST API's for converting an object to an XML while making a POST request to the server

Installation

$ npm install obj-to-xml

API

var objToXml = require('obj-to-xml');

objToXml.buildXmlFromObj(object, toString)

object

Pass in any object which has to be converted to xml/string. Object's property which is defined with @, is treated as an "attribute".

toString (Boolean) [optional]

If value is set to true, it returns an xml in "string format". If value is set to false, it returns an "XML". Defaults to false.

Usage
var objToXml = require("obj-to-xml");
var obj = {
    ROOT: {
        R: {
            "@id": 1
        }
    }
}
objToXml.buildXmlFromObj(obj);
/* Returns an XML, since toString is undefined.
<ROOT> <R id="1"/> </ROOT> */
 
objToXml.buildXmlFromObj(obj, true);
/* Returns XML string, since toString is set to true.
'<ROOT> <R id="1"/> </ROOT>' */
 

To get a list of items, pass an array as value to the object's property.

var objToXml = require("obj-to-xml");
var obj = {
    STUDENTS: {
        S: [{
            "@id": 1,
            "@name": "Nandeesh"
        }, {
            "@id": 2,
            "@name": "Stanley"
        }]
    }
}
objToXml.buildXmlFromObj(obj);
/* returns an XML,
<STUDENTS>
    <S id="1" name="Nandeesh"/>
    <S id="2" name="Stanley"/>
</STUDENTS>
*/
 

To add a text content to the tag, set #textContent# property to the object.

var objToXml = require("obj-to-xml");
var obj = {
    ROOT: {
        R: {
            "@id": 1,
            "#textContent#": "Lorem Ipsum"
        }
    }
}
objToXml.buildXmlFromObj(obj);
/* Returns an XML,
<ROOT>
    <R id="1">"Lorem Ipsum"</R>
</ROOT> */
 

NOTE:

This package can be used only for client-side applications like ReactJS/ AngularJS etc.

Install

npm i obj-to-xml

DownloadsWeekly Downloads

10

Version

1.0.5

License

ISC

Unpacked Size

4.74 kB

Total Files

3

Last publish

Collaborators

  • avatar