最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to parse xml file to array in JavaScript node.js - Stack Overflow

programmeradmin0浏览0评论

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=";>
    <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": "
    },                                                       
    "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));
    });
});

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/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/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));
    });
});
Share Improve this question edited Aug 21, 2021 at 19:57 Marcel 15.8k22 gold badges105 silver badges164 bronze badges asked Apr 21, 2016 at 8:09 DiPixDiPix 6,09318 gold badges70 silver badges121 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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
});
发布评论

评论列表(0)

  1. 暂无评论