我的javascript代码有问题,我需要从特定节点获取所有数据。我需要所有节点' ows_Person'显示在我的网站上。 我可以显示1' own_person'但我需要循环通过所有这些并显示它们。我不知道我是怎么想这样做的,因为我是这种东西的初学者。我会恭维一些帮助。
Javascript代码:
var xmlhttp, xmlDoc;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "test.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
//Zet de XML data in een lijst.
document.getElementById("Name").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].getAttribute('ows_Person');
//Zet de XML data in een lijst.
document.getElementById("ID").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].getAttribute('ows_ID');
//Zet de XML data in een lijst.
document.getElementById("Created").innerHTML=
xmlDoc.getElementsByTagName("z:row")[0].getAttribute('ows_Modified');
document.getElementById('users').innerHTML = output;
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>
答案 0 :(得分:1)
const transform = require('camaro')
const 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>
`
const { persons } = transform(xml, {
persons: ['xml/rs:data/z:row', {
ows_Person: '@ows_Person',
ows_ID: '@ows_ID',
ows_Modified: '@ows_Modified',
ows_Attachments: '@ows_Attachments',
ows_LinkTitle: '@ows_LinkTitle'
}]
})
console.log(JSON.stringify(persons, null, 2))
输出
[
{
"ows_Attachments": "0",
"ows_ID": "1",
"ows_LinkTitle": "Test",
"ows_Modified": "2017-11-15 15:00:22",
"ows_Person": "1257;#Stefan de Boer"
},
{
"ows_Attachments": "0",
"ows_ID": "2",
"ows_LinkTitle": "testtesttesttest",
"ows_Modified": "2017-11-16 10:46:07",
"ows_Person": "1256;#Kevin"
},
{
"ows_Attachments": "0",
"ows_ID": "3",
"ows_LinkTitle": "nog een test",
"ows_Modified": "2017-11-16 10:53:11",
"ows_Person": "1258;#Naomi"
},
{
"ows_Attachments": "0",
"ows_ID": "4",
"ows_LinkTitle": "alweer een test",
"ows_Modified": "2017-11-16 10:53:44",
"ows_Person": "1257;#Stefan"
}
]