解析XML并返回NaN而不是实际值?

时间:2018-05-02 19:16:44

标签: javascript xml

我正在尝试获取MonthDataLoaded代码的值,我希望它返回Apr,但是当我提醒变量时它会返回NaN。如果我抓住YearDataLoaded并提醒它,则会返回正确的2018值。是什么给了什么?

XML结果:

<BMSHealthReport>

<Site>Ft. Belvoir</Site>

<TotalUnits>349</TotalUnits>

<PercentReporting>85.39 %</PercentReporting>

<PercentNotReporting>14.61 %</PercentNotReporting>

<PercentBypassed>0.29 %</PercentBypassed>

<NumberLogins>52</NumberLogins>

<NumberAlarms>871</NumberAlarms>

<TotalEnergySavings>22,498</TotalEnergySavings>

<MonthDataLoaded>Apr </MonthDataLoaded>

<YearDataLoaded>2018</YearDataLoaded>

</BMSHealthReport>

我如何抓住我想要的特定价值:

monthLoaded = xml.responseXML.xmlDoc.getElementByTagName("BMSHealthReport").parseFloat(x[1].getElementsByTagName("MonthDataLoaded")[0].childNodes[0].nodeValue);

alert(monthLoaded);

1 个答案:

答案 0 :(得分:1)

您正在尝试将字符串解析为float,它会返回NaN非类似数字的输入。删除parseFloat电话:

monthLoaded = xml.responseXML.xmlDoc.getElementsByTagName("BMSHealthReport")[0].getElementsByTagName("MonthDataLoaded")[0].childNodes[0].nodeValue);