I have an xml file that looks like this:
<!DOCTYPE ROOT SYSTEM "zombie.dtd">
<ROOT>
<row>
<field name="buildid">26960</field>
<field name="cast(status as char)">Filesystem 1K-blocks Used Available Use% Mounted on
C:cygwinin 285217976 88055920 197162056 31% /usr/bin
C:cygwinlib 285217976 88055920 197162056 31% /usr/lib
C:cygwin 285217976 88055920 197162056 31% /
c: 285217976 88055920 197162056 31% /cygdrive/c
d: 285217976 88055920 197162056 31% /cygdrive/d</field>
<field name="eventtime">2011-05-25 16:26:09</field>
<field name="schedulerid">13</field>
</row>
and I am trying to parse out the individual fields (buildid, status, eventtime, etc) but I am getting none as the result of all of my function calls, here is what I am doing:
log("Parsing XML file...")
try:
xml = ET.parse(xml_file)
except Exception, inst:
print "Unexpected error opening %s: %s" % (file, inst)
sys.exit(1)
log("Parsing Complete")
#store the root of the element tree
root = xml.getroot()
zombies = []
for zombie in root.findall('row/'):
#read the zombie data
buildID = zombie.get('buildid')
print buildID
status = zombie.get('cast(status as char')
print status
eventTime = zombie.get('eventtime')
print eventTime
schedulerID = zombie.get('schedulerid')
print schedulerID
#create a zombie object
#z = Zombie(buildID, status, eventTime, schedulerID)
#zombies.append(z)
can someone tell me what I am doing wrong?
Thanks
Your
zombieis a<row>element, not the<field>element that you want. When you callget()on it, you are trying to get an attribute on the<row>element, but what you want is the node value of the<field>element with that id.If you have Python 2.7, you can use ElementTree’s partial XPath support to find the correct element: