Hello friends, now a days its very common requirement for the conversion from XML to JSON, if you are using an Web Services and XML as a response. Its very simple to use JSON data in our UI development. When we need this conversion actually ?, very simple answer is that when we have a Web Service, which has the response or output as XML data, before using it in our UI, we can convert this XML response to JSON data, so that it will be easy for us to represent these data on our UI.
In this post we are going to learn following points,
- How can we convert XML to JSON
- Steps to convert XML to JSON
How can we convert XML to JSON :
In Node JS world there are so many modules or packages are available for our required implementations. For this XML to JSON conversion, so many modules are available, but in this post we are going to use one of the simple module that is xml2js module. So by using this module we can convert a XML data to JSON data.
Steps to convert XML to JSON:
Following are the step by step procedure to use xml2js module and to convert XML to JSON,
Step 1: (Install xml2js)
We can install this module xml2js by using npm, type the following command in command prompt or shell respectively,
> npm install xml2js --------------> For windows
or
> $npm install xml2js -------------> For linux
Step 2: (Create a XML file test.xml)
If you don't have any XML Web Service URL, then create a XML file as name as text.xml by writing following XML code,
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Step 3: (Write the code for XML to JSON conversion)
Now we have an XML file as in above step, so we will write the code for converting XML to JSON as follows,
var fs = require('fs'), // File System Module
var xml2js = require('xml2js'); // XML2JS Module
var parser = new xml2js.Parser(); // Creating XML to JSON parser object
// Reading and Parsing the file
fs.readFile(__dirname + '/test.xml', function(err, data) {
parser.parseString(data, function (err, result) {
console.log(result);
console.log('Done');
});
});
Write the above code in a .js file, named as xml2json.js.
Step4: (Run the Application)
Now our code is ready, so we need to run the application for seeing the result. To run this application, first go the application placed directory in command prompt or shell and type following command,
> node xml2json.js --------------> For windows
or
> $node xml2json.js -------------> For linux
You will see the following JSON response or output,
I had tried with xml2json but it doesn't work well then I found good tutorial node js convert xml to json using xml2js.
ReplyDeletehttp://guruquest.net/question/node-js-convert-xml-to-json/
Nice..
ReplyDeletecheckout this link for xml2json
check out this link for xml2json conversion
https://cloudtechnzly.blogspot.com
But the con side of xml2js is that every values is getting converted to array.
ReplyDelete{
"executionStatus": [{
"status": ["OK"],
"description": ["Search/Retrieve done successfully"],
"dataObject": ["Records 1..1 of 1 found"]
}],
"persons": [{
"ID": ["654654654"],
"nAME": ["udhay"]
}]
}
do not work properly
ReplyDeletevery nice blog thanks for the valuable information, i got an idea on Node JS
ReplyDeleteNode JS Online training
Node JS training in Hyderabad