4

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>
6
  • 1
    In which browser(s) do you get the error? Where is placeMarks defined? Commented Nov 30, 2010 at 17:01
  • Wow, nothing like deleting comments. Especially my one that asked for more details. I love people editing and not paying attention. Commented Nov 30, 2010 at 17:26
  • 1
    Lets do this again. What browser? What is the full Error Message? What is the line number? Commented Nov 30, 2010 at 17:27
  • 1
    You are not getting errors that how can you say i am getting this error: Invalid number of parameters. Sounds like you are getting an error. :) Commented Nov 30, 2010 at 18:33
  • hi....can any one guide me how to read the "employee.XML" if its onserver.. Commented Mar 21, 2012 at 12:19

2 Answers 2

11

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;
   ......
}
Sign up to request clarification or add additional context in comments.

5 Comments

Philar: how would you loop in xmlhttp.open("GET"
Chicagoland, I think the mistake that you are making is that the xml tag should be <emp id="1007"> not <empid="1007">. Hence while parsing it should be xmlDoc.getElementsByTagName("emp")
After making this change if you need the value of the attribute id you should use xmlDoc.getElementsByTagName("emp")[0].attributes.getNamedItem("id").nodeValue
Thanks Philar: but how do i loop through the xml file; i will update the xml file to get you an idea what i am talking about.
i have just updated the xml file what i want to do is to loop throught the xml file and grab the values.
0

Use xmlhttp.send(null); http://www.w3.org/TR/XMLHttpRequest/#the-send-method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.