1

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.

asked Apr 1, 2013 at 21:22
1
  • I tried the code you posted, and it worked. Are you trying that exact same code, or something different? Commented Apr 1, 2013 at 21:40

1 Answer 1

2

Your xml is invalid. There needs to be a single root node to be valid xml. responseXML will only be populated if the response is valid xml.

answered Apr 1, 2013 at 21:26
Sign up to request clarification or add additional context in comments.

3 Comments

@shankshera is the content type of the file set to text/xml
I believe you're right, it must be the content type. I tried the OP code using a .xml file, apache served it with the xml content type, and it worked. Renamed the file to .owl, and got the exact same error as the OP.
Thanks, this helped me... My responseText and response variables were populating after request but ResponseXML was null. After editing the XML, my requestXML variable populated as I expected.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.