30

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?

4
  • Use the node-xml2js package. npmjs.com/package/xml2js Commented Sep 11, 2018 at 17:47
  • 1
    First of all, welcome to SO! Second of all, make sure you research your issues before you post questions on here - it'll increase your chances of getting answered and upvoted if the question is strong. Commented Sep 11, 2018 at 17:55
  • Possible duplicate of Any recommendation for xml to json for Node.js? Commented May 31, 2019 at 17:56
  • You can give simple-xml-to-json a try, it's pretty compact, and has no external dependencies. npmjs.com/package/simple-xml-to-json Commented Apr 25, 2020 at 19:16

9 Answers 9

40

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the package in the accepted answer and I got this error: xml not formated well, but xml-js package worked perfectly. Thanks
33

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.

2 Comments

xml2json is not updated and work on old dependency and do not install on nodejs
It is deprecated and is not supported, It will not work on windows 11 for sure, It will fail on node-expat, they have posted solution on github/Readme.md Install Visual Studio C++ 2012 and run npm with the --msvs_version=2012 flag. but you cannot find or install visual studio c++ 2012 Microsoft doesn't support so cant use it anymore. npmjs.com/package/node-expat
18

If you are choosing between xml2json and xml-js then as far as I understand the differences are:

  • xml-js has much less dependencies and uses sax-js for xml parsing.
  • xml2json has more dependencies including node-expat that requires python and can be a headache during npm i. But node-expat claims to be ~3 times faster than sax-js.

Also be aware that xml2json and xml-js produce a bit different JSON. When I replaced xml2json with xml-js I had to add "._attributes" everywhere where values were in attributes.

Comments

8

In 6 simple ES6 lines:

xml2json = xml => {                                                                                                                                                     
  var el = xml.nodeType === 9 ? xml.documentElement : xml                                                                                                               
  var h  = {name: el.nodeName}                                                                                                                                          
  h.content    = Array.from(el.childNodes || []).filter(e => e.nodeType === 3).map(e => e.textContent).join('').trim()                                                  
  h.attributes = Array.from(el.attributes || []).filter(a => a).reduce((h, a) => { h[a.name] = a.value; return h }, {})                                                 
  h.children   = Array.from(el.childNodes || []).filter(e => e.nodeType === 1).map(c => h[c.nodeName] = xml2json(c))                                                    
  return h                                                                                                                                                              
}

Test with echo "xml2json_example()" | node -r xml2json.es6 with source at https://github.com/brauliobo/biochemical-db/blob/master/lib/xml2json.es6

1 Comment

This loses mixed-content and element ordering (attribute ordering is at parser's choice anyways). Many disavow mixed-content anyways so.. Not a problem, just some users might care.
3

Cruftless allows you to 'annotate' the data structure you want to match, to specify how it should be bound to a JSON representation of that same data.

So, if you define your template like this:

<TestScenario>
  <TestSuite name="{{name}}"><?bind suites|array?>
    <TestCaseName name="{{name}}">{{data}}</TestCaseName><?bind cases|array?>
  </TestSuite>
</TestScenario>

Then by calling .fromXML on the object created from it, passing in the XML you want to parse, you will get:

{
  suites: [
    { name: 'tc_Logout', data: 'dt_EdgeCaseRoute' },
    {
      name: 'tc_AddContract',
      data: 'dt_EdgeCaseHome,dt_EdgeCaseSpectrum'
    },
    { name: 'tc_Payment', data: 'dt_EdgeCaseRoute' },
    { name: 'tc_ClientFeedback', data: 'dt_EdgeCaseSpectrum' }
  ]
}

1 Comment

Trying to use it but the documentation is a bit thin on the notation and I could find no other examples of using fromXML in this way there.
3

var fs = require('fs');

var xml2json = require('xml2js');

var parser = new xml2json.Parser();
var i = 0;
fs.readFile('note.xml', function(err,data){
    parser.parseString(data, function(err, result){  
        console.log(result);
    });
});

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
2

You can also try camaro

const { transform } = require('camaro')

const 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>
`

;(async function () {
    const result = await transform(xml, {
        testSuites: [
            '/TestScenario/TestSuite',
            {
                name: '@name',
                testCases: ['TestCaseName', {
                    name: '@name',
                    data: '.'
                }]
            }
        ]
    })

    console.log(JSON.stringify(result, null, 2))
})()

output:

{
  "testSuites": [
    {
      "name": "TS_EdgeHome",
      "testCases": [
        {
          "name": "tc_Login",
          "data": "dt_EdgeCaseHome,dt_EdgeCaseRoute"
        },
        {
          "name": "tc_Logout",
          "data": "dt_EdgeCaseRoute"
        }
      ]
    },
    {
      "name": "TS_EdgePanel",
      "testCases": [
        {
          "name": "tc_AddContract",
          "data": "dt_EdgeCaseHome,dt_EdgeCaseSpectrum"
        }
      ]
    },
    {
      "name": "TS_EdgeRoute",
      "testCases": [
        {
          "name": "tc_VerifyContract",
          "data": "dt_EdgeCaseRoute"
        },
        {
          "name": "tc_Payment",
          "data": "dt_EdgeCaseRoute"
        }
      ]
    },
    {
      "name": "TS_EdgeSpectrum",
      "testCases": [
        {
          "name": "tc_ClientFeedback",
          "data": "dt_EdgeCaseSpectrum"
        }
      ]
    }
  ]
}

Comments

1

You can also make a bash script, for example:

#!/usr/bin/env bash

cat path/to/localfile.xml | xml2json | jq .

and then import the output json to your node project.

Comments

-1

if you have a XML file, then you can read that XML file using fs in nodejs and then you can use "xml2json" npm package.

    const parser = require('xml2json');
    const fs = require("fs")



    fs.readFile(uploadedFilePath, function(err,data){

        if(err) {
            return res.send({message:err});
        } else {

            jsonfile = JSON.parse(parser.toJson(data,{reversible: true}));

        }

    });

1 Comment

There is already an answer suggesting usage of the same tool with explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.