XML ElementTree Parse.

M

marc.wyburn

I'm playing with XML and elementtree and am missing something but I'm
not sure what...? I've create an XML file with Elementtree with a root
of backup.xml. Attached to the root is a dirob and the dirobj has a
fileobj. fileobj has filename and filesize tags. I can open the file
in excel and it sets out the columns as I would expect. The problem
I'm having is parsing the file. Using..

Doesn't return anything, infact I can't seem to find anything
regardless of what I search for.
Here is the XML. I've got a list of files and their containing
directories and I want my python script to append to the XML file each
day. I could have done it with SQL but fancied banging my head on the
wall instead. Thanks, MW.


- <backup.xml>
- <dirob>
<dirname>C:\sysprep</dirname>
- <fileob>
<filename>test.txt</filename>
<filesize>10</filesize>
</fileob>
</dirob>
</backup.xml>
 
F

Fredrik Lundh

I'm playing with XML and elementtree and am missing something but I'm
not sure what...? I've create an XML file with Elementtree with a root
of backup.xml. Attached to the root is a dirob and the dirobj has a
fileobj. fileobj has filename and filesize tags. I can open the file
in excel and it sets out the columns as I would expect. The problem
I'm having is parsing the file. Using..


Doesn't return anything, infact I can't seem to find anything
regardless of what I search for.

find/findtext/findall only look at direct subelements, unless you use xpath-
style syntax. in this case,

elem.findtext("dirob/fileob/filesize")

should do the trick. or you could do something like

for dir_elem in tree.findall("dirob"):
for file_elem in dirob.findall("fileob"):
print file_elem.findtext("filesize")

to loop over all directory objects at the top level, and print the sizes for
all files in those directories.

to get the first filesize in the tree, no matter where it is, use

elem.findtext(".//filesize")
 
M

marc.wyburn

Thanks Fredrik, thats got me started but just incase anyone looks there
is a slight mistype in your code...

or you could do something like
for dir_elem in tree.findall("dirob"):
for file_elem in dirob.findall("fileob"):
print file_elem.findtext("filesize")

to loop over all directory objects at the top level, and print the sizes for
all files in those directories.

should read
.... for felem in delem.findall("fileob"):
.... print felem.findtext("filesize")
....
933888
9365
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top