Parse XML in Node js

There are many different Node.js libraries for parsing XML, each with its own characteristics and use cases. Another library that’s hugely popular is xml2js. You can, however, look into the others as well: fast-xml-parser and xmldom. For this example, I’ll use xml2js and fast-xml-parser, which happen to be two of the most common libraries for XML parsing in Node.js.

Using xml2js
Step 1. Installxml2js

First of all, install the xml2js package via npm by using the following code in your command prompt:

npm install xml2js

Step 2. Write a Script to Parse XML

Create a file with the name parseXml.js and use the following details for creating this file:

const fs = require('fs');
const xml2js = require('xml2js');

// Create new parser instance
const parser = new xml2js.Parser();

// Read XML data from a file. Otherwise, you might want to use another source.
fs.readFile('example.xml', 'utf8', (err, data) => {
if (err)
console.error('Error reading XML file:',
return;

// Parse XML data
Parse the string of data using parser.parseString(data, (err, result) => { if (err) { console.error('Error parsing XML:', err); return; } // Output the result console.log('Parsed XML data:', JSON.stringify(result, null, 2)); }); });

 

Step 3. Create an Example XML File

Create a file called example.xml which contains some sample XML content like the file given below:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Aman</to>
<from>Jenny</from>
<note>
<heading>Reminder
<body>Don't forget outing with me this weekend!</body>
</note>

 

Step 4. Run the Script

Now, run your script using Node.js:

 

node parseXml.js

 

You should see the parsed data of the XML in JSON format.

Till now, we have parsed XML in Node.js using the ‘xml2js’ library.

Now we’ll do it using fast-xml-parser

Step 1. Install fast-xml-parser

Install the fast-xml-parser package using the code given below in your command prompt:

npm install fast-xml-parser
Step 2. Create Script for Parsing XML

Create a parseXmlFast.js file with the following content:

const fs = require('fs');
const { XMLParser } = require('fast-xml-parser');

// new parser instance
const parser = new XMLParser();

// Read XML data from a file
fs.readFile('example.xml', 'utf8', (err, data) => {
if (err) {
console.error('Error reading XML file:', err);
return;
}

// Parse XML data
const jsonObj = parser.parse(data);

// Output the result
console.log('Parsed XML data:', JSON.stringify(jsonObj, null, 2));
});

 

Step 3. Run the Script

Now run your script with Node.js:

 

node parseXmlFast.js

 

You should obtain an output containing the parsed XML data in JSON format.

Summary

While doing the job of parsing XML in Node.js, xml2js and fast-xml-parser have yet another set of features or performance characteristics. On one hand, xml2js is typically noted for its flexibility and usability. On the other hand, fast-xml-parser shall push on performance and support additional XML features.

Use the library most appropriate according to your needs, and don’t hesitate to reach out and try both in order to use the one fitting your special use case. In case of further questions or seeking assistance, do not hesitate to ask in the comments!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top