SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up 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
    Last edited by jemrusalem07; Mar 25, 2010 at 23:58. Reason: added the products.xml

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if you use this?
    Code JavaScript:
    if (xmlDoc.documentElement && xmlDoc.documentElement.hasChildNodes()) {
        ...
    }
    Birnam wood is come to Dunsinane


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •