无法为我的XML找到正确的节点值

时间:2017-11-16 10:22:20

标签: javascript html xml

我正在制作一个必须向我的网站显示xml数据的项目,但我卡住了。我需要弄清楚如何从'z:row'显示'ows_Person'。我不知道如何在[0] .ChildNodes [0] .nodeValue中定义它,我将不胜感激。

Javascript代码:

<script>
var xmlhttp, xmlDoc;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "test.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.getElementById("name").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].childNodes[0].nodeValue;
document.getElementById("ID").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].childNodes[0].nodeValue;
document.getElementById("Title").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].childNodes[0].nodeValue;
</script>

XML代码:

<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-
microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30">
<s:AttributeType name="ows_Attachments" rs:name="Attachments" rs:number="1">
<s:datatype dt:type="boolean" dt:maxLength="1" />
</s:AttributeType>
<s:AttributeType name="ows_LinkTitle" rs:name="Title" rs:number="2">
<s:datatype dt:type="string" dt:maxLength="512" />
</s:AttributeType>
<s:AttributeType name="ows_Person" rs:name="Person" rs:number="3">
<s:datatype dt:type="string" dt:lookup="true" dt:maxLength="512" />
</s:AttributeType>
<s:AttributeType name="ows_ID" rs:name="ID" rs:number="4">
<s:datatype dt:type="i4" dt:maxLength="4" />
</s:AttributeType>
<s:AttributeType name="ows_Modified" rs:name="Modified" rs:number="5">
<s:datatype dt:type="datetime" dt:maxLength="8" />
</s:AttributeType>
<s:AttributeType name="ows_DocIcon" rs:name="Type" rs:number="6">
<s:datatype dt:type="string" dt:maxLength="512" />
</s:AttributeType>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ows_Attachments="0" ows_LinkTitle="Test" ows_Person="1257;#Stefan de 
Boer" ows_ID="1" ows_Modified="2017-11-15 15:00:22" />
<z:row ows_Attachments="0" ows_LinkTitle="testtesttesttest" 
ows_Person="1256;#Kevin" ows_ID="2" ows_Modified="2017-11-16 10:46:07" 
/>
<z:row ows_Attachments="0" ows_LinkTitle="nog een test" 
ows_Person="1258;#Naomi" ows_ID="3" ows_Modified="2017-11-16 
10:53:11" />
<z:row ows_Attachments="0" ows_LinkTitle="alweer een test" 
ows_Person="1257;#Stefan" ows_ID="4" ows_Modified="2017-11-16 
10:53:44" />
</rs:data>
</xml>

1 个答案:

答案 0 :(得分:3)

试试getAttribute

xmlDoc.getElementsByTagName("z:row")[0].getAttribute('ows_person')