Python and XML help

Mathieu mathieu at nouvelempire.net
Fri Jul 26 13:03:43 EDT 2002


Hello,

I'm new to Python and having some problems parsing the following XML :

<properties name="spam">
<prop>
<first>
 <item name="foo">
 <item name="foo2">
</first>

<second>
 <item name="bar">
 <item name="bar2">
</second>
</prop>

I use the xmllib module based on an example I've seen in the archives
of this newsgroup :

import xmllib, string

class myparser(xmllib.XMLParser):
    def __init__(self):
        xmllib.XMLParser.__init__(self)
        self.currentdata = []

    def handle_data(self, data):
        self.currentdata.append(data)

    def start_first(self,attrs):
        print "List of the items:"

    def start_item(self,attrs):
        print attrs['name']

    def end_item(self):
        pass

    def end_first(self):
        pass

if __name__ == "__main__":
    p=myparser()
    p.feed(data)
    p.close()

I would like the script to write only the "name" variables that are
within the "first" brackets and ignore the others. Please note that my
code must stay compatible with Python 1.5 and that this example might
not work or contain errors, because it is a simplified version of the
code I have written.

Thanks for your help!



More information about the Python-list mailing list