Using XMLHttpRequest, reading XML data
i am trying to read the xml file but somehow i am getting this error: Invalid number of parameters.
<script type="text/javascript">
//<![CDATA[
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", "employee.XML", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var empid= xmlDoc.getElementsByTagName("empid");
var total = placeMarks.length;
var names = xmlDoc.getElementsByTagName("Name");
var designation= xmlDoc.getElementsByTagName("designation");
var phone= xmlDoc.getElementsByTagName("phone");
.....
</script>
XML data:
<employee>
<emp id="1007">
<name>John Chamber</name>
<designation>Web Expert1</designation>
<phone>555-55-555</phone>
<name>John D</name>
<designation>Web123123</designation>
<phone>555-55-555</开发者_高级运维phone>
<name>Chamber</name>
<designation>Web Expert</designation>
<phone>555-55-555</phone>
<name>Thomas</name>
<designation>TESTTEST</designation>
<phone>555-55-555</phone>
</emp>
</employee>
Firstly branch out your code as shown below to see if the server is responding with the correct 200
response.
xmlhttp.open("GET", "employee.XML", false);
xmlhttp.send(null);
if (xmlhttp.status==200) {
xmlDoc = xmlhttp.responseXML;
var empid= xmlDoc.getElementsByTagName("emp");
var total = placeMarks.length;
var names = xmlDoc.getElementsByTagName("Name");
var designation= xmlDoc.getElementsByTagName("designation");
var phone= xmlDoc.getElementsByTagName("phone");
..... ;
}
else if (xmlhttp.status==404) {
alert("XML could not be found");
}
Also my suggestion is to use a javascript library like jQuery which does much of the heavy lifting for your. The whole code for creating the XHR object simply reduces to one line
$.get(url,function(data){
xmlDoc = data;
var empid= xmlDoc.getElementsByTagName("emp");
var total = placeMarks.length;
var names = xmlDoc.getElementsByTagName("Name");
var designation= xmlDoc.getElementsByTagName("designation");
var phone= xmlDoc.getElementsByTagName("phone");
});
Finally whichever modern browser that you are using should easily be able to point out which line in the script is throwing the error. If you still face the issue please confirm which line is throwing the error.
EDIT The problem scope has changed. The OP now wants to loop through the xml. First of all the xml needs to be designed keeping the requirements in mind. Hence it should look like this
<employees>
<emp id="006">
<name>John Chamber</name>
<designation>Web Expert1</designation>
<phone>555-55-555</phone>
</emp>
<emp id="007">
<name>John D</name>
<designation>Web123123</designation>
<phone>555-55-555</phone>
</emp>
<emp id="008">
<name>Chamber</name>
<designation>Web Expert</designation>
<phone>555-55-555</phone>
</emp>
<emp id="009">
<name>Thomas</name>
<designation>TESTTEST</designation>
<phone>555-55-555</phone>
</emp>
</employees>
Now the javascript for parsing the xml
var emp=xmlDoc.getElementsByTagName("emp");
for (i=0;i<emp.length;i++) {
var names=emp[i].childNodes[0].text;
var designation= emp[i].childNodes[1].text;
......
}
Use xmlhttp.send(null);
http://www.w3.org/TR/XMLHttpRequest/#the-send-method
精彩评论