XML Parsing Help,

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Fri Jun 29 06:14:23 EDT 2007


Hello Chaps,

 

I'm looking for some help with XML parsing, I've been playing around with
this over the past few days and the only solution I can come up with seems
to be a little slow and also leaves what I think is a memory leak in my
application, which causes all kinds of problems. 

 

I have a very simple XML file which I need to loop over the elements and
extract the attribute information from, but the loop must be conditional as
the attributes must meet a certain criteria.

 

My current solution is using minidom, which I've read isn't one of the
better parsers, if anyone knows of any that are better for the task I would
love to hear it, the content is extracted regularly so whatever we chose
needs to be quick. Take a look at this brief example of the XML we're
dealing with:

 

<schedules name="Default event" location="this is the location of the
event">

                <event name="This is an event" location="At my house"
type="1" />

                <event name="And Another" location="At work" type="2" />

                <event name="This is some more" location="At the cafe"
type="1" />

                <event name="And one last one" location="At my house"
type="3" />

</schedules>

 

Now what I need to do is loop through the 'event' elements and locate the
first one which has a type of '2' and return the name and location, if it is
unable to find any events with the type of 2 then I want it to return the
default event which is defined by the attributes of the schedules element.

 

I'm pretty inexperienced with parsing XML in python and could really use
some help selecting a parser and writing the code, I'm sure it's all quite
simple I'm just looking for the best solution.

 

Thanks guys,

 

Rob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070629/7efead5a/attachment.html>


More information about the Python-list mailing list