a blog for those who code

Friday 4 September 2015

How to convert an object to XML in Nodejs

In this post we are going to discuss about how to convert an object to XML and XML to object in NodeJS. In my previous post I have discussed about converting an object to JSON. Since JSON is a string-based representation of a JavaScript object, it is pretty straight forward. XML is actually less convenient but there are times where developer have to work with it.

We will be using xml2js module as it provides a basis for us to interact with XML by converting it to JavaScript object. According to xml2js, it is a simple XML to JavaScript object converter. It supports bi-directional conversion. To install xml2js you need to install it using npm through the command npm install xml2js.

Like the previous post, we will at first create a module for us which will give us an object. Let's name the file as objects.js. The content of objects.js are as shown below.

module.exports = {
  nodeBook1 : {
    bookName: "Professional Node.js",
    author: "Pedro Teixeria",
    buy: "http://www.amazon.in/Professional-Node-JS-Building-Javascript-Based-Scalable/dp/8126539283/ref=as_sl_pc_tf_mfw?&linkCode=wey&tag=codidefi-21"
  },
  nodeBook2 : {
    bookName: "Node.Js In Action",
    author: "Mike Cantelon, Marc Harter, T.J. Holowaychuk",
    buy: "http://www.amazon.in/Node-Js-In-Action-Mike-Cantelon/dp/9351192601/ref=as_sl_pc_tf_mfw?&linkCode=wey&tag=codidefi-21"
  },
  nodeBook3 : {
    bookName: "Angular JS",
    author: "Brad Green",
    buy: "http://www.amazon.in/Angular-JS-Brad-Green/dp/9351101266/ref=as_sl_pc_tf_mfw?&linkCode=wey&tag=codidefi-21"
  },
  nodeBook4 : {
    bookName: "Professional ASP NET MVC",
    author: "Jon Galloway",
    buy: "http://www.amazon.in/Professional-ASP-NET-MVC-Jon-Galloway/dp/1118794753/ref=as_sl_pc_tf_mfw?&linkCode=wey&tag=codidefi-21"
  }
}

Our object contains the details of some book. If you are not familiar of creating modules then check Creating Own Custom Module in Nodejs. Now we are ready to create our main file where we are going to do the conversion. At first we need to get the object from objects.js file which can be done using require. We are going to use require to dynamically load our object at initialization. The full code to convert the object to XML is shown below.

var objects = require('./objects'),
xml2js = require('xml2js');

var xmlValues = new xml2js.Builder({rootname : 'objects'});
convertedObjects = xmlValues.buildObject(objects);
console.log(convertedObjects);

xml2js has a builder constructor which will loop through our object and all sub-objects and then convert all properties into parent XML nodes, and all non-object values into text XML nodes. The above code will give the output as shown below.


Now we are going to convert the XML back to the object. For doing that we are going to use xml2js.parseString() as shown below.

xml2js.parseString (convertedObjects, {
    explicitArray: false,
    explicitRoot: false
}, function(err, obj) {
    console.log(obj);
});

The above code will give us an output as shown below.


You can download the objects.js and convert.js files from GitHub. Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment