0

I'm learning how to do this with the help of some tutorials but this code is not working. The browser is not displaying anything from the XML file. Is there anything wrong with my code? Here are the codes:

<?xml version="1.0"?>
<Company>
  <Employee category="technical">
    <FirstName>Tanmay</FirstName>
    <LastName>Patil</LastName>
    <ContactNo>1234567890</ContactNo>
  </Employee>
  <Employee category="non-technical">
    <FirstName>Taniya</FirstName>
    <LastName>Mishra</LastName>
    <ContactNo>1234667898</ContactNo>
  </Employee>
</Company>

<!DOCTYPE html>
<html>
 <body>
   <div>
     <b>FirstName:</b> <span id="FirstName"></span><br>
     <b>LastName:</b> <span id="LastName"></span><br>
     <b>ContactNo:</b> <span id="ContactNo"></span><br>
     <b>Email:</b> <span id="Email"></span>
   </div>
   <script>
     if (window.XMLHttpRequest) {
      //if browser supports XMLHttpRequest
     // Create an instance of XMLHttpRequest object. code  for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp = new XMLHttpRequest();
     } else {// code for IE6, IE5
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     // sets and sends the request for calling "node.xml"
     xmlhttp.open("GET","company.xml",false);
     xmlhttp.send();
     // sets and returns the content as XML DOM
     xmlDoc=xmlhttp.responseXML;
    //parsing the DOM object

     document.getElementById("FirstName").innerHTML=xmlDoc.getElementsByTagName("FirstName")[0].childNodes[0].nodeValue;
     document.getElementById("LastName").innerHTML=xmlDoc.getElementsByTagName("LastName")[0].childNodes[0].nodeValue;
     document.getElementById("ContactNo").innerHTML=xmlDoc.getElementsByTagName("ContactNo")[0].childNodes[0].nodeValue;
     document.getElementById("Email").innerHTML=xmlDoc.getElementsByTagName("Email")[0].childNodes[0].nodeValue;

    </script>
  </body>
</html>
2
  • do you have any errors in the console? Commented Nov 10, 2014 at 5:00
  • Nope i think it was an error in the xml file itself. Commented Nov 10, 2014 at 6:53

3 Answers 3

1

You need to move the xml code into another file. I think you code is correct. I have just moved the xml to a new .xml file named company.xml in the site root directory and it is working fine.

<?xml version="1.0"?>
<Company>
<Employee category="technical">
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
</Employee>
<Employee category="non-technical">
<FirstName>Taniya</FirstName>
<LastName>Mishra</LastName>
<ContactNo>1234667898</ContactNo>
</Employee>
</Company>
Sign up to request clarification or add additional context in comments.

5 Comments

every thing is displaying fine except the Email, which is not present in the xml.
The xml part is already in another file but still it's not displaying anything. Can i know which browser you used?
Chrome. may be the javascript is not getting the path of your XML file. Please check it in chrome and if it is not working I will send you my working zip file.
It's still not working. Can you send me the zip file? It would be much appreciated.
Thanks it is [email protected]
0
   if (window.XMLHttpRequest){ //if browser supports 
    XMLHttpRequest
    {
    xmlhttp = new XMLHttpRequest();
    }
    }
    else
    {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

a pair of curly brace missing.

Comments

0

I tried the following code, and it is working fine. Hope your code is correct. You can move the XML data to some other file and check once. I hope your code should work fine.

function loadFile() {
var xmlhttp;
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","popup.xml",true);
xmlhttp.send();

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        responseData = xmlhttp.responseText;
    }
}

}

Comments

Your Answer

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.