1

I have an HTML5 file with javascript to read in a local XML file.

One element in the XML structure is dynamic and contains inner xml, no value.

When trying to display this nodename on the webpage all I see is #text.

Example XML:

<Students>
    <student id="1">
        <Connor>
           <age>20</age>
           <gender>male</gender>
        </Connor>
    </student>
    <student id="2">
        <Fiona>
           <age>25</age>
           <gender>female</gender>
        </Fiona>
    </student>
</Students>

Javascript:

var x = xmlDoc.getElementsByTagName("student");

for(i = 0; i < x.length; i++)
{
    var id = x[i].getAttribute("id");

    var name = x[i].childNodes[0].nodeName;        // produces '#text'
    // var name = x[i].firstChild.nodeName;        // produces '#text'   

    document.write("<p>name = " + x[i].childNodes[0].nodeName + "</p>");  // produces '#text'

    var age = x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue;
    var gender = x[i].getElementsByTagName("gender")[0].childNodes[0].nodeValue;

    var student= {id:id, name:name, age:age, gender:gender };
}

In my XML example I understand that the name of the student could be better stored but this is just an example based on a fixed XML structure I'm working with.

Furthermore: reading and displaying the XML file only works whilst running through the IDE. I would eventually like to be able to run the .html file and have the javascript code read the xml file (stored in the same location as the html file) and displays its data.

Edit: First part is solved. Still could use help on the "Furthermore" above.

tjheslin1
  • 1,378
  • 6
  • 19
  • 36

2 Answers2

1

Ahh this old chestnut, I had the same problem, try

 var name = x[i].children[0].nodeName; 

This should only get the first xml.

Basicallly the problem is in your list you have 0:text 1:XMLNode, 2:XMLnode ect.

And to get only xml stuff, you can use children rather then childNodes, very similar to HTML Elements EDIT this example doesn't work

The actual answer:

You need to check the typeof child by using typof when looping throught

Jay
  • 1,033
  • 1
  • 18
  • 31
  • Hi Jay. Thanks for responding. It doesn't seem to like `x[i].children[0].nodeName` :S I tried using `write()`, first printing a test message, then your suggested line and then another test message. All I see is the first test message suggesting `children` is undefined. – tjheslin1 Nov 09 '14 at 15:34
  • I have solved this now and have posted a link to a related question. You were correct about the nodeType so thank you. :D – tjheslin1 Nov 09 '14 at 15:42
  • Yes apologies, You should use typeof when looping through to check that its an actual node – Jay Nov 09 '14 at 15:45
0

I have found a suitable answer for the first part of my question which appears to be a duplicate of:

Javascript/XML - Getting the node name

I've only seen this now in the related questions to my own. It wasn't shown to me when searching or when writing my question. So props to @T.J. Crowder

my solution is:

var nameNode = x[i].firstChild;

while(nameNode.nodeType != 1)
{
    nameNode = nameNode .nextSibling;
}

var name = nameNode;
Community
  • 1
  • 1
tjheslin1
  • 1,378
  • 6
  • 19
  • 36