Monday, 2 September 2013

Trying to parse XML in python using ElementTree?

Trying to parse XML in python using ElementTree?

I'm fairly new to python, but I'm trying to create an API for a website.
However, I'm having trouble accessing an attribute of the XML document I'm
trying to parse.
The file looks like this:
<WORLD>
<NEWNATIONS>
nation1,nation2,nation3,nation4,nation5,etc,etc
</NEWNATIONS>
</WORLD>
How do i access the nations in that XML file, preferably as a list? Again,
I'm completely new to Python, but I've been scouring the internet for
answers and I can't find any.
My code looks like this right now:
import xml.etree.ElementTree as ET
from urllib2 import urlopen
response = urlopen("SITE URL")
tree = ET.parse(response)
root = tree.getroot()
for child in root:
print child.attrib
So... I can get it to print "World", I can get it to print "NEWNATIONS",
but I can't get it to print the nations within the "NEWNATIONS" tag. Help?

No comments:

Post a Comment