Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Parse An XML Response With Node.js

TwitterFacebookRedditLinkedInHacker News

A while back I wrote an article on how to parse XML using PHP. Since then I’ve slowly been transitioning away from PHP, yet XML continues to be a burden that I can’t get rid of.

Not all APIs return JSON so it is the application developers responsibility to handle the XML. Lucky for us there is a convenient package for Node.js called xml2js that will handle all the parsing for us.

Following the trend of the PHP tutorial, we are going to use the same data as seen below:

<?xml version="1.0" encoding="UTF-8" ?>
    <company>Code Blog</company>
    <owner>Nic Raboy</owner>

We’re going to assume that you have already installed Node.js on your computer.

Create a new project directory and add a JavaScript file to it that will handle our parsing. If you are using a Terminal, do something like this:

mkdir TestApp
cd TestApp
touch app.js

At this point we need to install the xml2js library, so in your Terminal, enter the following:

npm install xml2js

If successful, you should now have a node_modules directory in your project with xml2js inside.

Now it is time to crack open your app.js file and add the following code:

var parseString = require('xml2js').parseString;
var xml = '<?xml version="1.0" encoding="UTF-8" ?><business><company>Code Blog</company><owner>Nic Raboy</owner><employee><firstname>Nic</firstname><lastname>Raboy</lastname></employee><employee><firstname>Maria</firstname><lastname>Campos</lastname></employee></business>';
parseString(xml, function (err, result) {

It is now time to run the Node.js code. From the Terminal, use the following to execute the code:

node app.js

If everything was successful, you should get a usable JSON response.

    "business": {
        "company": [ "Code Blog" ],
        "owner": [ "Nic Raboy" ],
        "employee": [
                "firstname": [ "Nic" ],
                "lastname": [ "Raboy" ]
                "firstname": [ "Maria" ],
                "lastname": [ "Campos" ]

Above is the JSON response you should receive. One thing that threw me off was that all XML string elements turned into JSON arrays. I’m more familiar in seeing { owner: "Nic Raboy" } rather than { owner: [ "Nic Raboy" ] }, but it isn’t a big deal.

If you’re creating a web service that hits an XML API, it is always best to convert to JSON from your users. Languages such as AngularJS were designed to use JSON.

This xml2js library can be used in your Express.js or SailsJS application if you’re using one of those as a framework.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.