繁体   English   中英

XML属性在Node.js xml2js中不起作用

[英]XML Attribute not working in Node.js xml2js

我正在尝试使用Node.js和xml2js解析XML。 在文档中说$是访问属性的字符。 在我看来,它似乎不起作用。

对象result.ApiResponse.CommandResponse可以正常工作。 但是,我之后提出的任何内容都不确定。

这是我的代码,它说$是未定义的:

var xml2js = require('xml2js');
var util = require('util');

var parser = new xml2js.Parser();
var xml = '<ApiResponse Status="OK"><Errors/><Warnings/><RequestedCommand>namecheap.domains.check</RequestedCommand><CommandResponse Type="namecheap.domains.check"><DomainCheckResult Domain="us.xyz" Available="true" ErrorNo="0" Description="" IsPremiumName="true" PremiumRegistrationPrice="13000.0000" PremiumRenewalPrice="13000.0000" PremiumRestorePrice="65.0000" PremiumTransferPrice="13000.0000" IcannFee="0.0000" EapFee="0.0000"/></CommandResponse><Server>PHX01APIEXT01</Server><GMTTimeDifference>--5:00</GMTTimeDifference><ExecutionTime>4.516</ExecutionTime></ApiResponse>';

parser.parseString(xml, function (err, result) {

console.log(util.inspect(result.ApiResponse.CommandResponse.DomainCheckResult.$.Available, false, null))    

});

这是console.log(result):

{ ApiResponse: 
   { '$': { Status: 'OK' },
     Errors: [ '' ],
     Warnings: [ '' ],
     RequestedCommand: [ 'namecheap.domains.check' ],
     CommandResponse: 
      [ { '$': { Type: 'namecheap.domains.check' },
          DomainCheckResult: 
           [ { '$': 
                { Domain: 'us.xyz',
                  Available: 'true',
                  ErrorNo: '0',
                  Description: '',
                  IsPremiumName: 'true',
                  PremiumRegistrationPrice: '13000.0000',
                  PremiumRenewalPrice: '13000.0000',
                  PremiumRestorePrice: '65.0000',
                  PremiumTransferPrice: '13000.0000',
                  IcannFee: '0.0000',
                  EapFee: '0.0000' } } ] } ],
     Server: [ 'PHX01APIEXT01' ],
     GMTTimeDifference: [ '--5:00' ],
     ExecutionTime: [ '4.516' ] } }

看起来CommandResponseDomainCheckResult实际上是数组,因此在深入研究数据之前,需要使用[0]访问它们的第一个元素。

console.log(util.inspect(
    result.ApiResponse.CommandResponse[0].DomainCheckResult[0].$.Available,
    false, null
))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM