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

How to use APIs with Python Forum

View Course

669 points
5151e483f82b88b41c00030d_686803971
Submitted by
Warren Reid
about 3 years ago

firstChild.nodeValue

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

permalink

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

http://www.w3schools.com/dom/dom_nodetree.asp

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:
pets
pet
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:

     pets.getElementsByTagName('name')

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
4e1cffcbbf7f2ece4c3abfda4ea1756d?s=140&d=retro
Submitted by
CJ Bradbury
almost 3 years ago