3

I'm beginner and I have problem with parse data from xml file to array in Node.js?

Data from xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
       <Name>Apple</Name>
       <Code>AP</Code>
       <Price>2,5</Price>
    </record>
    <record>
       <Name>Kiwi</Name>
       <Code>KI</Code>
       <Price>1,5</Price>
    </record>
</xml>

And i expect array like this:

var array = [
    { Name: 'Apple', Code: 'AP', Price: '2,5'},
    { Name: 'Kiwi', Code: 'KI', Price: '1,5'}
];

@EDIT - we're closer

I was trying to use xml2js npm, result was:

 {                                                            
  "xml": {                                                   
    "$": {                                                   
      "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance
    },                                                       
    "record": [                                              
      {                                                      
        "Name": [                                            
          "Apple"                                            
        ],                                                   
        "Code": [                                            
          "AP"                                               
        ],                                                   
        "Price": [                                           
          "2,5"                                              
        ]                                                    
      },                                                     
      {                                                      
        "Name": [                                            
          "Kiwi"                                             
        ],                                                   
        "Code": [                                            
          "KI"                                               
        ],                                                   
        "Price": [                                           
          "1,5"                                              
        ]                                                    
      }                                                      
    ]                                                        
  }                                                          
}

My current code:

var fs = require('fs');
var parseString = require('xml2js').parseString;

fs.readFile('test_data.xml', function(err, data){
    parseString(data, function (err, result)
    {
       console.log(JSON.stringify(result, null, 2));
    });
});

1 Answer 1

2

Check out one of the many XML Parser Packages on npm.

For example: xml2js

var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
    console.dir(result);
});

If you need help reading the xml File in, check out the nodeJs File System Libary.

Doc for reading a file:

var fs = require('fs');

fs.readFile('/etc/data.xml', (err, data) => {
  if (err) throw err;
  console.log(data);
});

EDIT: To make it all in one function:

function loadXML(cb) {
  fs.readFile('test_data.xml', function(err, data){
    parseString(data, function (err, result) {
      cb(result.xml.record)
    });
  });
}

loadXML(function(yourRecods) {
  // do whatever
});
Sign up to request clarification or add additional context in comments.

6 Comments

@DiPix can you post what JSON.stringify(result, null, 2); returns?
just use var records = result.xml.record; and you get the array
Thank you :) should be enough
One more thing, how can i assign this function to some variable? I tried: var xmlData = fs.readFile('data-xml/test_data.xml', function (err, data) { parseString(data, function (err, result) { return result.xml.record; }); }); but it's undefinded when i'll trying to use :/
Check my edit, you need a function with a callback as parameter
|

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.