0

I'm trying to retrieve some data from an xml file using ajax

  <?xml version="1.0" encoding=UTF-8"?>

  <user>
     <u_idno>1</u_idno>
     <u_name>nobody</u_name>
     <u_srnm>nothing</u_srnm>
     <u_role>linux</u_role>
  </user>

  <user>
     <u_idno>2</u_idno>
     <u_name>yesbody</u_name>
     <u_srnm>something</u_srnm>
     <u_role>administrator</u_role>
  </user>

but I am getting an error

Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

I am unsure why it would say null and I've been googling furiously to find out what I've done wrong but I'm clueless. My javascript is as followed

  function f_ajax() {

     var lv_request;

     try {
        lv_request = new XMLHttpRequest();
     } catch (error) {
        lv_request = new ActiveXObject("Microsoft.XMLHTTP");
     }

     lv_request.onreadystatechange = function() {

        if(lv_request.readyState == 4 && lv_request.status == 200) {

           lv_xml = lv_request.responseXML;
           lv_row = lv_xml.getElementsByTagName("user");
           lv_output = null;

           for (lv_cnt = 0; lv_cnt < lv_row.length; lv_cnt++) {

              lv_output = lv_output + lv_row[lv_cnt].childNodes[0].nodeValue;

           }

           document.getElementById("h2_ajax").innerHTML = lv_row;

        }

     }

     lv_request.open("GET", "data.xml", true);
     lv_request.send();

  };

  f_ajax();
5
  • 1
    What's in lv_request.responseText? Commented Jun 11, 2015 at 19:38
  • It outputs the entire xml file Commented Jun 11, 2015 at 19:41
  • If the value you are seeing in responseText is not fully-valid XML you will see a NULL in responseXML. Commented Jun 11, 2015 at 19:56
  • Take a look at the following post: stackoverflow.com/questions/3781387/responsexml-always-null -- in particular the answer that talks about parsing the responseText back to an XML document (i.e. the answer from Kenny). Commented Jun 11, 2015 at 19:59
  • Your opening <u_role> tag doesn't match the closing </role> tag. Is that a copying error or in the real file? Commented Jun 11, 2015 at 20:09

1 Answer 1

1

Your XML is not well formed. It is missing a root and has some other issues. Try this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user>
   <u_idno>1</u_idno>
   <u_name>nobody</u_name>
   <u_srnm>nothing</u_srnm>
   <u_role>linux</u_role>
 </user>
 <user>
   <u_idno>2</u_idno>
   <u_name>yesbody</u_name>
   <u_srnm>something</u_srnm>
   <u_role>administrator</u_role>
 </user>
</root>
Sign up to request clarification or add additional context in comments.

1 Comment

Those incorrect closing tags were a typo in my question but it was the root tags that fixed it, I didn't know I needed a master parent, thanks so much

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.