Hello,
Few days back I worked on task to read XML file in NodeJs and parse the data and convert data to the JSON format. Here in this blog I am going explain how I did achieved this.
Following is our sample XML file.
<root>
<parent>
<firstchild>First Child Content 1</firstchild>
<secondchild>Second Child Content 1</secondchild>
</parent>
<parent>
<firstchild>First Child Content 2</firstchild>
<secondchild>Second Child Content 2</secondchild>
</parent>
</root>
Step 1 : Install necessary NPM packages
First install following packages in your NodeJs application.
npm install --save xmldom
mpm install --save hashmap
Here xmldom is the package we are going to use to parse the XML data and hasmap package we are going to use to convert and save data in JSON format.
Step 2 : Import necessary packages in script
const { readFile } = require('fs/promises');
const xmldom = require('xmldom');
const HashMap = require('hashmap');
We are going to use readFile from fs/promises. Also we will use xmldom to parse the string data. xmldom is a A JavaScript implementation of W3C DOM for Node.js, Rhino and the browser. Fully compatible with W3C DOM level2; and some compatible with level3. Supports DOMParser and XMLSerializer interface such as in browser.
Step 3 : Read the XML file
const readXMLFile = async()=> {
var parser = new xmldom.DOMParser();
const result = await readFile('./data.xml',"utf8");
const dom = parser.parseFromString(result, 'text/xml');
}
Here we are using async function to read the XML file as we want to wait till XML file is completely finished. Also we created a xmldom.DOMParser which we will use to parse the string data of the file. This parser will give you all the TAGs of the XML file just like we access standard HTML tags. With this we can use getElementsByTagName to get XML tags.
Step 4 : Convert XML data to hasmap
const readXMLFile = async()=> {
var parser = new xmldom.DOMParser();
const dataMap = new HashMap();
const result = await readFile('./data.xml',"utf8");
const dom = parser.parseFromString(result, 'text/xml');
var parentList = dom.getElementsByTagName("parent");
for(var i =0; i < parentList.length; i++) {
const parent = parentList[i];
const firstchild = parent.getElementsByTagName("firstchild")[0].textContent;
const secondchild = parent.getElementsByTagName("secondchild")[0].textContent;
const parentMap = new HashMap();
parentMap.set("firstchild", firstchild);
parentMap.set("secondchild", secondchild);
dataMap.set(i, parentMap);
}
}
No comments:
Post a Comment