3

I'm trying to test if the xml file have the tag "<group>"

var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml", false);
xmlhttp.send(null);
xml = xmlhttp.responseXML.documentElement; 

var thegroup = xml.getElementsByTagName('group')[0];
if (!group) {
    alert('No <group> in the XML: ' + xml);
    return;
} else {
    alert(xml + 'have a <group> tag');
}

Even if my xml file have the tag "<group>" the result is always negative, and the variable "thegroup" is undefined.

"xml" give me "[object Element]"

Where is my mistake?

PS: I'm only interested in webkit, I don't care about IE, Opera or Firefox for now.

EDIT : HERE IS MY ACTUAL CODE

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
<title>xmltest</title>
<script type="text/javascript">

function init() {

var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml");
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;

function callbackFunction(){
  if (xmlhttp.readyState == 4){
     xml = xmlhttp.responseXML.documentElement; 
    var group = xml.getElementsByTagName('group')[0];
    console.debug(xml)
    if (!group) {
      alert('No <group> in the XML: ' + xml);
      return;
    } else {
      alert(xml + 'have a <group> tag');
    }
  }
}

};

</script>
</head>
<body onLoad="init();">

</body>
</html>

and my xmlfile.xml :

<?xml version="1.0" ?>

<group type="vertical">
<name>name</name>
<title>title</title>
</group>

At this point the alert is triggered saying : No <group> in the XML: [object Element]

So maybe my problem is just on the way I try to find the <group> tag ?

1
  • 1
    Abstract away the AJAX. Make the input to your testcase a hard-coded string. Commented Feb 5, 2012 at 21:10

2 Answers 2

8

XMLHttpRequest is asynchronous, it doesn't work that way. When you use xmlhttp.send(null); you have to define callback function that will be executed when the server responds with the data, otherwise you are trying to access empty data. The code would look something like this:

var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", "xmlfile.xml");
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;



function callbackFunction(){
  if (xmlhttp.readyState == 4){
     xml = xmlhttp.responseXML.documentElement; 
    var thegroup = xml.getElementsByTagName('group')[0];
    if (!group) {
      alert('No <group> in the XML: ' + xml);
      return;
    } else {
      alert(xml + 'have a <group> tag');
    }
  }
}

this way, you are using onReadyStateChange to tell the browser to run callbackFunction everytime the server sends back a response. It tests for the readyState to be 4 which means that the request has been completely served.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks but I got "Uncaught ReferenceError: xml is not defined" for xml.getElementsByTagName('group')[0]; ...
-1: No, this is a synchronous request. See his third argument to .open. And yours, incidentally; onreadystatechange won't even be invoked.
Sorry, my mistake. I edited the answer so to move the line xml = xmlhttp.responseXML.documentElement; to inside the if clause in the callback
You are right @lightness, i didn' see that. So the answer is really 'group' vs 'thegroup' as you answered, thanks for pointing that out
if I put an alert alert(thegroup); right after "var thegroup = xml.getElementsByTagName('group')[0];" I get "thegroup is not defined"
|
1
var thegroup = xml.getElementsByTagName('group')[0];
if (!group) {
    alert('No <group> in the XML: ' + xml);
    return;
} else {
    alert(xml + 'have a <group> tag');
}

What is group? Did you mean thegroup?

4 Comments

group is the xml tag '<group>Lorem</group>' in xmlfile.xml that I want to test. I can change the variable 'thegroup' to 'group' it doesn't change anything.
@Julian: It does change something: it'll mean that you're not testing something that doesn't exist in your if condition.
Yeah you're right. I have to test on the variable name, not the tag name. so the correct variable name should be 'group' not 'thegroup'. But it still doesn't change anything, because 'group' is still undefined if I put an alert before the if
@Julian: No, the variable name is thegroup. When you do var, you're assigning to thegroup. But then in the if you're checking on group, which does not exist. You meant to write if (!thegroup)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.