我正在尝试使用 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(结果):
{ 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' ] } }