0

I'm using this code:

<script type="text/javascript">
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","css/galerii.xml",false)
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.getElementsByTagName("GALERIE")[0].childNodes[0].nodeValue);
</script>

to process some xml:

<?xml version="1.0" encoding="UTF-8" ?>
<GALERIES>
<GALERIE>
info
</GALERIE>

<GALERIE>
other info
</GALERIE>
</GALERIES>

But I get nothing in the alert and shouldn't xmlhttp.open("GET","css/galerii.xml",false) have a value if it's successful ? It's undefined. There's a root node now, same result.

4 Answers 4

2

You have no root node (document element), which is a requirement in XML.

<?xml version="1.0" encoding="UTF-8" ?>
<GALERIES>
  <GALERIE>
    info
  </GALERIE>

  <GALERIE>
    other info
  </GALERIE>
</GALERIES>

You also have no onreadystatechange method for your AJAX request. When your code which reads the responeXML executes, the http request for the XML has yet to return. You need to read up on how to build AJAX requests: https://developer.mozilla.org/en/xmlhttprequest

Working example: http://jsfiddle.net/2F8q6/1/

Your JS modified to work as the example does:

<script type="text/javascript">
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open( "GET","css/galerii.xml", false );

    xmlhttp.onreadystatechange = function()
    {
        if( xmlhttp.readyState === 4 && xmlhttp.status === 200 )
        {
            xmlDoc=xmlhttp.responseXML;
            alert(xmlDoc.getElementsByTagName("GALERIE")[0].childNodes[0].nodeValue);
        }
    }

    xmlhttp.send();
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

I got the same result even with a root node.
Thank you for the answer. I made a new page just with this script in the body and I get nothing. What could be wrong. By the way shouldn't xmlhttp.open( "GET","css/galerii.xml", false ); have true as the third parameter ? I tried both ways but still nothing.
At this point I don't know what else may be wrong with your script. If you're able to make a version of the broken script available for access online, shoot me a link and I'll take a look.
I tried putting another alert inside the if() and I didn't get a response so I thought one of the conditions is never met. I deleted xmlhttp.status === 200 and it went through I got my alert but not the xml alert. Any ideas ? What alternatives do I have if this doesn't work ?
readyState of 4 means the request is complete, status is the HTTP status that was returned from the server. When readyState is 4, if status is not 200 something went wrong with the request. What are you getting for status when readyState is 4?
|
1

XML document may only have one root element.

Comments

1

You need a single root element.

<?xml version="1.0" encoding="UTF-8" ?>
<GALERIES>
  <GALERIE>
    info
  </GALERIE>
  <GALERIE>
    other info
  </GALERIE>
</GALERIES>

Comments

0

fix your xml . your xml is not valid. there is no root element in your xml. try here http://www.w3schools.com/Dom/dom_validate.asp.

Comments

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.