Basic AJAX: hasChildNodes() method

Hi Experts,

Please help me as I study ajax.
I encounter the error message:
“hasChildNodes() is not a method”

Here is my source code:

var xmlName = ‘products.xml’;
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open(“GET”,xmlName,false);
}
else
{
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.load(xmlName);
}

//error method here
if(xmlDoc.hasChildNodes())
{
var xmlcode = new Array(xmlDoc.getElementsByTagName(“product”).lengt h);

Here is the products.xml:

<products>
<product>
<code>a1</code>
<desc>laptop</desc>
</product>
</products>

Please help me why I encounter this error message?

Thank you very much,
Jemaru

Does it behave any better when the XML file starts with an XML doctype?

I also see at http://www.w3schools.com/Dom/dom_parser.asp some code that might provide better results for you when loading an XML document.

What if you use this?

if (xmlDoc.documentElement && xmlDoc.documentElement.hasChildNodes()) {
    ...
}