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?
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.
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.
Convert XML → JS object / JSON. To convert XML text to JavaScript object, use xml2js() . To convert XML text to JSON text, use xml2json() .
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With