使用javascript从XML文件中获取数据

时间:2014-11-10 04:48:56

标签: javascript xml

我正在学习如何在一些教程的帮助下完成此操作,但此代码无效。浏览器不显示XML文件中的任何内容。我的代码有什么问题吗? 以下是代码:

<?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>

3 个答案:

答案 0 :(得分:1)

您需要将xml代码移动到另一个文件中。我认为你的代码是正确的。我刚刚将xml移动到站点根目录中名为company.xml的新.xml文件,它运行正常。

<?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>

答案 1 :(得分:0)

   if (window.XMLHttpRequest){ //if browser supports 
    XMLHttpRequest
    {
    xmlhttp = new XMLHttpRequest();
    }
    }
    else
    {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

缺少一对大括号。

答案 2 :(得分:0)

我尝试了以下代码,它运行正常。希望你的代码是正确的。 您可以将XML数据移动到其他文件并检查一次。 我希望你的代码能正常工作。

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;
    }
}

}