-1

I am trying to parse xml data with javascript in ajax call.But responseXML is returning null value.Here is my code

<script language="javascript">
if(window.addEventListener)
{
    window.addEventListener("load",getXML,false);
}
else if(window.attachEvent)
{
    window.attachEvent("onload",getXML);
}
function getXML()
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET","myxml.xml",true);
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4)
        {
            //alert("got response");
            var root = xhr.responseXML;
            alert(root);
        }
    }
    xhr.send(null);
}
</script>

Here is my "myxml.xml" file

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
    <child-1>
        <grandChild1_1>textOfGrandChild1_1</grandchild1_1>
        <grandChild2_1>textOfGrandChild2_1</grandchild2_1>
        <grandChild3_1>textOfGrandChild3_1</grandchild3_1>
    </child-1>
    <child-2>
        <grandChild1_2>textOfGrandChild1_2</grandchild1_2>
        <grandChild2_2>textOfGrandChild2_2</grandchild2_2>
        <grandChild3_2>textOfGrandChild3_2</grandchild3_2>
    </child-2>
    <child-3>
        <grandChild1_3>textOfGrandChild1_3</grandchild1_3>
        <grandChild2_3>textOfGrandChild2_3</grandchild2_3>
        <grandChild3_3>textOfGrandChild3_3</grandchild3_3>
    </child-3>
</root>

When I tried with

alert(xhr.responseText)

it showed the xml file as it is.But when using responseXML it is giving null value.Where is the problem?

Siva Charan
  • 95
  • 3
  • 15
  • Is it a valid xml file? – epascarello Apr 10 '15 at 18:25
  • i think it is.i gave my xml file,can you tell me if there is any wrong in that xml file – Siva Charan Apr 10 '15 at 18:28
  • @SivaCharan: You've received some nice answers to the 9 questions you've asked on Stack Overflow. Please review and accept some of them if they've helped you. Accepting an answer helps future readers by indicating that the answer worked for you. It also increases the asker's and answer's reputation, reflecting their helpfulness over time. Read how to accept and more about accepting [**here**](http://meta.stackoverflow.com/q/5234/234215). Thank you. – kjhughes Apr 10 '15 at 21:29

2 Answers2

1

Your XML was incorrect, beacuse it is case sensitive, changed grandchild1_1 to grandChild1_1 and it worked.

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<child-1>
    <grandChild1_1>textOfGrandChild1_1</grandChild1_1>
    <grandChild2_1>textOfGrandChild2_1</grandChild2_1>
    <grandChild3_1>textOfGrandChild3_1</grandChild3_1>
</child-1>
<child-2>
    <grandChild1_2>textOfGrandChild1_2</grandChild1_2>
    <grandChild2_2>textOfGrandChild2_2</grandChild2_2>
    <grandChild3_2>textOfGrandChild3_2</grandChild3_2>
</child-2>
<child-3>
    <grandChild1_3>textOfGrandChild1_3</grandChild1_3>
    <grandChild2_3>textOfGrandChild2_3</grandChild2_3>
    <grandChild3_3>textOfGrandChild3_3</grandChild3_3>
</child-3>

Here is the Plunker

Thanks!

netrevisanto
  • 1,091
  • 12
  • 13
1

Your XML is invalid, Case matters

 <grandChild1_1>textOfGrandChild1_1</grandchild1_1>
       ^                                  ^

All the closing tags have the wrong camel case in the names.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    You should say his XML is [*not well-formed* rather than *invalid*](http://stackoverflow.com/a/25830482/290085). – kjhughes Apr 10 '15 at 18:55