Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XML to JSON with NodeJS

I'm trying to convert the following xml to json, thereby I need to get a mapping to the TS-tc-dt

Here is the xml

<?xml version="1.0" encoding="UTF-8"?>
<TestScenario>
   <TestSuite name="TS_EdgeHome">
      <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgePanel">
      <TestCaseName name="tc_AddContract">dt_EdgeCaseHome,dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
      <TestSuite name="TS_EdgeRoute">
      <TestCaseName name="tc_VerifyContract">dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Payment">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgeSpectrum">
      <TestCaseName name="tc_ClientFeedback">dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
</TestScenario>

How can I achieve this in NodeJS?

like image 813
Dinesh Avatar asked Sep 11 '18 17:09

Dinesh


People also ask

Can I convert XML to JSON?

To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.

Can we convert XML to JSON in JavaScript?

Converting XML to JSON is not a tricky task. You can make complex equations simple by using this method. Several libraries can help to convert XML to JSON. Users can use two top methods to change markup language to JavaScript.

How to convert XML to JSON object in JavaScript?

Convert XML → JS object / JSON. To convert XML text to JavaScript object, use xml2js() . To convert XML text to JSON text, use xml2json() .

How parse XML file in NodeJs?

The installed module exposes the Parser object, which is then used to read XML. const xml2js = require('xml2js'); const fs = require('fs'); const parser = new xml2js. Parser({ attrkey: "ATTR" }); // this example reads the file synchronously // you can read it asynchronously also let xml_string = fs. readFileSync("data.


2 Answers

I've used xml-js - npm to get the desired result.

First of all I've installed xml-js via npm install xml-js

Then used the below code to get the output in json format

var convert = require('xml-js');
var xml = require('fs').readFileSync('./testscenario.xml', 'utf8');

var result = convert.xml2json(xml, {compact: true, spaces: 4});
console.log(result);
like image 138
Dinesh Avatar answered Oct 12 '22 15:10

Dinesh


You can use xml2json npm for converting your xml in to json. xml2json.

Step 1:- Install package in you project

npm install xml2json

Step 2:- You can use that package and convert your xml to json

let xmlParser = require('xml2json');
let xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<TestScenario>
   <TestSuite name="TS_EdgeHome">
      <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgePanel">
      <TestCaseName name="tc_AddContract">dt_EdgeCaseHome,dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
      <TestSuite name="TS_EdgeRoute">
      <TestCaseName name="tc_VerifyContract">dt_EdgeCaseRoute</TestCaseName>
      <TestCaseName name="tc_Payment">dt_EdgeCaseRoute</TestCaseName>
   </TestSuite>
   <TestSuite name="TS_EdgeSpectrum">
      <TestCaseName name="tc_ClientFeedback">dt_EdgeCaseSpectrum</TestCaseName>
   </TestSuite>
</TestScenario>`;

console.log('JSON output', xmlParser.toJson(xmlString));

Hope this might be helps to you.

like image 22
Dhiral Kaniya Avatar answered Oct 12 '22 15:10

Dhiral Kaniya