www.webdeveloper.com
Results 1 to 5 of 5

Thread: "firstChild.nodeValue"--nothing?!!

  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Angry "firstChild.nodeValue"--nothing?!!

    //here is my code.
    //XMLFile.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <peoples>
    <person>
    <name>A</name>
    <sex>M</sex>
    <mobile>11111111111</mobile>
    </person>
    <person>
    <name>B</name>
    <sex>F</sex>
    <mobile>22222222222</mobile>
    </person>
    </peoples>

    //js.js

    if(window.ActiveXObject){
    var doc=new ActiveXObject("Microsoft.XMLDOM");
    doc.async="false";
    doc.load("XMLFile.xml");
    }
    else if(document.implementation&&document.implementation.createDocument){
    var parser=new DOMParser();
    var doc=parser.parseFromString("XMLFile.xml", "text/xml");
    }
    else{
    throw new Error("XML DOM is not supported!");
    }

    var rs="";
    var name;
    var x=doc.getElementsByTagName('person');
    for(var i=0; i< x.length; i++){
    name=x[i].firstChild.firstChild.nodeValue;
    rs+=name + ", ";
    }
    document.write(rs);

    //why doesn't it work? Nothing is displayed in the browser. Why??
    //Help!!

  2. #2
    Join Date
    Jun 2012
    Location
    FL, USA
    Posts
    48
    ActiveX is out of date (and only worked for IE).... Use...
    Code:
    some_ajax_request = new XMLHttpRequest(); //Modern browsers support it.
    some_ajax_request.open("GET", yourxmlfile.xml);
    
    etc..
    http://www.w3schools.com/xml/xml_http.asp

  3. #3
    Join Date
    Mar 2011
    Posts
    1,156
    This line:
    Code:
    name=x[i].firstChild.firstChild.nodeValue;
    tries to fetch the first child of the first child in every 'person' node - in other words, a node contained within the first child node - and there aren't any. Each 'person' node in your data contains three child simple nodes. If you want the 'name' node, loop through those child nodes until you find it.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271

    Attention on the illegitimate children !

    There is different node types and some &#171; illegitime nodes &#187; like carriage returns, tabs or other specials characters which are to avoid to work with element Nodes (see, for example, this page about the different node Types).

    Then the two different following syntax give with Mozilla FireFox different firstChild Nodes !
    Code:
    // First syntax with line feed and carriage return
    <person>
    <name>A</name>
    <!-- -->
    </person>
    
    // Second syntax without characters between the two element Nodes
    <person><name>A</name>
    <!-- -->
    </person>
    Then to get the first element Node of your person tag (even if the code contains comments), you have to make something like this
    Code:
    var j=0,x=doc.getElementsByTagName('person');
    while (x.childNodes[j].nodeType!=1) j++;
    var xFirstElementNode=x.childNodes[j];
    Last edited by 007Julien; 07-25-2012 at 08:35 PM.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Sorry, I forgot the collection and the loop ! Replace x by x[i] in the last lines...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles