473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XML ElementTree Parse.

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
2 3004
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
4
by: alainpoint | last post by:
Hello, I use Elementtree to parse an elementary SVG file (in fact, it is one of the examples in the "SVG essentials" book). More precisely, it is the fig0201.svg file in the second chapter. The...
3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: rajarshi.guha | last post by:
Hi, recently having discovered ElementTree I'm stumped by a very simple problem, which I can't find the answer to. I have some XML in a string object. Now the parse() method of ElementTree takes...
0
by: sndive | last post by:
I have a weid problem. If i do this: import elementtree.ElementTree as ET .... tree = ET.parse("whatever") root = tree.getroot() r = root.find('last') print r return root where last is not an...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
2
by: =?ISO-8859-1?Q?J=2E_Pablo_Fern=E1ndez?= | last post by:
Hello, Is ElementTree supposed to load DTDs? I have some xmls heavy on entities and it fails this way: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright",...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.