This forum is now read-only. Please use our new forums at

669 points
Submitted by
Warren Reid
over 3 years ago


print name.firstChild.nodeValue

print name

What does the top value do - more specifically (firstChild.nodeValue). Print name returns an error - print name.firstChild.nodeValue passed the exercise. What is the difference (aside from the obvious)? What does the added "firstChild.nodeValue - tell the API to do?

1 vote


I think this has to do a lot with how XML is structured.

I'm no expert with XML or python but here's what I understand from reading the above documentation in reference to the lesson.

The nodes refer to the "tags"in the pets.txt:
name - species

The top node is referred to the root, which in this case is pets. The root is the only node that has no parent node. The firstChild of pets is pet, the nextSibling would be pet and the lastChild would be pet. If we were treating pet as the parent element, name would be the firstChild and species would be the lastChild.(I'M NOT 100% SURE. But it seems to make sense for this lesson)

Let's start breaking down 'name.firstChild.nodeValue
The 'name.' is the variable that we created to call the following method:


For example if I wanted to get the values for species instead, I would first need to make changes to the '.getElementsByTagName()' method. I would also change the rest of the variables for readability, but I could get away with just changing the method parameter if I was feeling lazy.

    species = pets.getElementsByTagName('species')
        for speciman in species:
    print speciman.firstChild.nodeValue

This would output the values in the species node instead of the name node.

I can't figure out what 'firstchild' does in this case, but I can verify that nodeValues, is literally what it sounds like, the values of the node. For example, 'Jefferey' is the nodeValue of the node.

Hope this helps. Good luck.

747 points
Submitted by
CJ Bradbury
over 3 years ago