【问题标题】:xmlHttpRequest nodeValue always nullxmlHttpRequest nodeValue 始终为空
【发布时间】:2016-07-27 15:50:02
【问题描述】:

我正在尝试读取一个简单的 XML 文件,但一方面使用 nodeValue 总是得到 null,另一方面注意到子节点上有一些奇怪的行为。

首先是奇怪的行为: 这是我的 xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<productList>
<product>
    <productname>Keilspanner</productname>
    <artikelnummer>m12.123</artikelnummer>
    <productid>2001</productid>
    <objname>keilspanner</objname>
    <length>6</length>
    <width>5</width>
    <height>2.5</height>
</product>
<product>
    <productname>feste Backe</productname>
    <artikelnummer>m12.456</artikelnummer>
    <productid>3001</productid>
    <objname>festeBacke</objname>
    <length>6</length>
    <width>4.4</width>
    <height>2.5</height>
</product>
<product>
    <productname>ClampRail</productname>
    <artikelnummer>123</artikelnummer>
    <productid>1001</productid>
    <objname>clampRail</objname>
    <length>40</length>
    <width>5</width>
    <height>5</height>
</product>
<product/>
</productList>

当我像这样打印出“产品”的所有子节点时:

for(i=0; i<=14; i++){
    console.log(xmlDoc.getElementsByTagName("product")[0].childNodes[i]);
};

我明白了:

loadXML.js:22 #text
loadXML.js:22 <productname>​Keilspanner​</productname>​
loadXML.js:22 #text
loadXML.js:22 <artikelnummer>​m12.123​</artikelnummer>​
loadXML.js:22 #text
loadXML.js:22 <productid>​2001​</productid>​
loadXML.js:22 #text
loadXML.js:22 <objname>​keilspanner​</objname>​
loadXML.js:22 #text
loadXML.js:22 <length>​6​</length>​
loadXML.js:22 #text
loadXML.js:22 <width>​5​</width>​
loadXML.js:22 #text
loadXML.js:22 <height>​2.5​</height>​
loadXML.js:22 #text

每个第二个值实际上都是我的 xml 文件的一个子级。

第二个问题:当我打电话时:

xmlDoc.getElementsByTagName("product")[0].childNodes[1].nodeValue;

它总是返回 null。

这是我完整的js:

function readXml() {


var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "xml/products.xml", true);
xhttp.send();

}

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    for(i=0; i<=14; i++){
        console.log(xmlDoc.getElementsByTagName("product")[0].childNodes[i]);
    };

    console.log(xmlDoc.getElementsByTagName("product")[0].childNodes[1].nodeValue);
}

希望你能帮助我,我是 xml 新手,一开始我就失败了 :D

【问题讨论】:

    标签: xml xmlhttprequest


    【解决方案1】:

    每个第二个值实际上都是我的 xml 文件的一个子级。

    每个值都是第一个产品元素的子节点。

    每个第二个值都是一个子元素节点。其他的是文本节点

    元素之间有空格。空白是文本。文本创建节点。

    当我打电话时:

    xmlDoc.getElementsByTagName("product")[0].childNodes[1].nodeValue;
    

    它总是返回 null。

    这是意料之中的。 xmlDoc.getElementsByTagName("product")[0].childNodes[1]&lt;productname&gt;Keilspanner&lt;/productname&gt;,是一个元素节点。

    node value of an element is always null

    如果你想要其中的文本,那么你必须达到文本节点的值。

     xmlDoc.getElementsByTagName("product")[0].childNodes[1].firstChild.nodeValue
    

    【讨论】:

    • 感谢您的快速回复。所以你的意思是我的xml文件中的空白实际上是由错误的语法引起的?或者这很常见,我只需要使用不均匀的 childnumber 来获取我的值。
    • 语法没有什么不好。如果您只想获取元素节点,请检查循环中的节点类型,如果它不是元素,请检查 continue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2013-05-21
    • 1970-01-01
    • 2017-07-31
    • 2019-07-18
    • 2019-08-18
    相关资源
    最近更新 更多