By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

XML ElementTree Parse.

P: n/a
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..
>>file = open('c:\\scripts\\backup.xml', "r")
tree = parse(file)
elem = tree.getroot()
elem.findtext('filesize')
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>

Oct 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ma*********@googlemail.com wrote:
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..
>>>file = open('c:\\scripts\\backup.xml', "r")
tree = parse(file)
elem = tree.getroot()
elem.findtext('filesize')

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")

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


Oct 12 '06 #2

P: n/a
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 delem in tree.findall("dirob"):
.... for felem in delem.findall("fileob"):
.... print felem.findtext("filesize")
....
933888
9365

Oct 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.