如何从节点中的xml中获取此json结果?使用xml2js

时间:2018-09-10 22:45:42

标签: arrays json node.js string xml2js

我从服务器获取了一个xml响应,我需要获取之间的内容。我使用xml2js将xml转换为json,但是我将作为一个对象,而不是直接在下面的字符串。

xml response (what is in the JSON 'response' below):
...<response><results><result><zpid>21853941</zpid></result></results>...</response>

var xml2js = require('xml2js');
var parser = new xml2js.Parser();

 parser.parseString(body, function(err, result){
          console.log(result);
          console.log('----------------');
          var x = result["SearchResults:searchresults"]["response"] //this works
          //var x = result["SearchResults:searchresults"]["response"]["results"] //this doesn't
          console.log(x);

          x.forEach(function(e){ //this grabs object
              console.log('e: ' +e)
          });

      });

所以这是输出:

OUTPUT:
result:
 { 'SearchResults:searchresults':
 { '$':
  { 'xsi:schemaLocation': 'http://www.zillow.com/static/xsd/SearchResults.xsd https://www.zillowstatic.com/vstatic/b20c067/static/xsd/SearchResults.xsd',
    'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
    'xmlns:SearchResults': 'http://www.zillow.com/static/xsd/SearchResults.xsd' },
 request: [ [Object] ],
 message: [ [Object] ],
 response: [ [Object] ] } } // <-need to get what is in here
 ----------------
x:
[ { results: [ [Object] ] } ]

e: [object Object]

我需要掌握“响应”中的内容。

0 个答案:

没有答案