I have what should be an extremely simple xml question, which has me completely lost. I have the following XML file:
<items>
<item percentFinished="0.0">
<details>
<name>Objective 1</name>
<description>Test objective</description>
</details>
</item>
<item percentFinished="0.0">
<details>
<name>Objective 2</name>
<description>Another objective</description>
</details>
<subitems>
<item percentFinished="0.0">
<details>
<name>Sub Objective 1</name>
<description>A Sub Objective</description>
</details>
</item>
</subitems>
</item>
</items>
The javascript file is eventually going to be creating dynamic html from the xml file, but for now I am just trying to get the parsing down properly. Here is my current javascript file:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","defaultData.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
// HTML Div
var outer = document.createElement('div');
var div = document.createElement('div');
// Gets the item list from the XML
var itemList = selectValue(xmlDoc,"items/item");
var item = itemList.iterateNext();
while (item!=null) {
parseItemNode(item,div);
item = itemList.iterateNext();
}
outer.appendChild(div);
document.write(outer.innerHTML);
function parseItemNode(itemNode, parentNode) {
var details = selectValue(itemNode,"item/details").iterateNext();
var name = selectValue(details,"name").stringValue;
}
function selectValue(context,xpath) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
var nsResolver = document.createNSResolver( context.ownerDocument == null ? context.documentElement : context.ownerDocument.documentElement );
return document.evaluate(xpath, context, nsResolver, XPathResult.ANY_TYPE,null);
} else {
return context.selectNodes(xpath);
}
}
So the issue I am having is: the details variable in the parseItemNode() function is null. I had a look at the selectValue(itemNode,"item/details") call in the debugger, and the result is this:
XPathResult {invalidIteratorState: false, resultType: 4, iterateNext: function, snapshotItem: function, ANY_TYPE: 0…}
booleanValue: [Exception: TypeError: Failed to read the 'booleanValue' property from 'XPathResult': The result type is not a boolean.]
invalidIteratorState: false
numberValue: [Exception: TypeError: Failed to read the 'numberValue' property from 'XPathResult': The result type is not a number.]
resultType: 4
singleNodeValue: [Exception: TypeError: Failed to read the 'singleNodeValue' property from 'XPathResult': The result type is not a single node.]
snapshotLength: [Exception: TypeError: Failed to read the 'snapshotLength' property from 'XPathResult': The result type is not a snapshot.]
stringValue: [Exception: TypeError: Failed to read the 'stringValue' property from 'XPathResult': The result type is not a string.]
__proto__: XPathResult
At this point, I have no idea what is going on or how to fix it, and I haven't been able to dig up anything on Google. Can someone explain to me what is happening, and how I should make it work correctly?