This is a bit strange. I ran this script in different computer and got different result. In one computer, this code worked but didn't work in another. Here's my js code:
xmlDoc = loadXmlDoc();
printSubClassOf();
function loadXmlDoc(){
var xmlDom = null;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "resto.owl", false);
xhttp.send(null);
xmlDom = xhttp.responseXML;
return xmlDom;
}
function printSubClassOf(){
a = xmlDoc.getElementsByTagName("SubClassOf");
for(i = 0; i < a.length; i++){
b = xmlDoc.getElementsByTagName("SubClassOf")[i].childNodes;
for(j = 0; j < b.length; j++){
c = xmlDoc.getElementsByTagName("SubClassOf")[i].childNodes[j];
document.write(i + " " + j + " " + c.nodeName + "<br>");
}
}
}
And this is the XML I tried to parse.
<Ontology>
<SubClassOf>
<Class IRI="#Chef"/>
<Class IRI="#Employee"/>
</SubClassOf>
<SubClassOf>
<Class IRI="#Customer"/>
<Class IRI="#People"/>
</SubClassOf>
</Ontology>
I used Chrome Developer Tools to see what was going on. There was a XHR request with 200 status code but still got this error.
a = xmlDoc.getElementsByTagName("SubClassOf");
Uncaught TypeError: Cannot call method 'getElementsByTagName' of null
I think printSubClassOf()
finished before loadXmlDoc()
returns the XML. What do you think? And how to solve this problem? FYI: I ran this on localhost.