Using xmllib
Robert Roy
rjroy at takingcontrol.com
Wed Apr 12 22:45:58 EDT 2000
On Wed, 12 Apr 2000 13:38:41 +0200, Pieter Claerhout
<PClaerhout at CREO.BE> wrote:
>Hello,
>
>does anyone has an example on how to use the xmllib to parse an xmlfile?
>I have an xml-file, which looks a bit like this:
>
><!-- a general comment -->
><COLLECTION VERSION="1">
> <GENERAL PATH="S:\" />
> <ITEM NAME="Item1">
> <DESCRIPTION>Item 1 description.</DESCRIPTION>
> <DATE>Thu Dec 16 16:35:05 1999</DATE>
> </ITEM>
> <ITEM NAME="Item2">
> <DESCRIPTION>Item 2 description.</DESCRIPTION>
> <DATE>Wed Mar 15 11:54:27 2000</DATE>
> </ITEM>
></COLLECTION>
>
>I want to convert it to something like this:
>
>Collection from path s:\
>
>- Item 1 (Thu Dec 16 16:35:05 1999)
> Item 1 description.
>- Item 2 (Wed Mar 15 11:54:27 2000)
> Item 2 description.
>
>Anyone an idea on how to do that with XMLlib?
>
>Kind regards,
>
>
>Pieter
>Pieter_Claerhout at creoscitex.com
>
This is a fairly trivial example based on what you posted.
Bob
++++++++++
import xmllib, string
class myparser(xmllib.XMLParser):
def __init__(self):
xmllib.XMLParser.__init__(self)
self.currentdescrip = ''
self.currentdate = ''
self.currentitem = ''
self.currentdata = []
def handle_data(self, data):
self.currentdata.append(data)
def start_COLLECTION(self, attrs):
print "Collection from path",
def end_COLLECTION(self):
pass
def start_GENERAL(self, attrs):
print attrs['PATH']
def end_GENERAL(self):
pass
def start_ITEM(self, attrs):
self.currentitem = attrs['NAME']
def end_ITEM(self):
print '-%(currentitem)s (%(currentdate)s)\n
%(currentdescrip)s' % self.__dict__
def start_DESCRIPTION(self, attrs):
self.currentdata = []
def end_DESCRIPTION(self):
self.currentdescrip = string.join(self.currentdata,'')
def start_DATE(self, attrs):
self.currentdata = []
def end_DATE(self):
self.currentdate = string.join(self.currentdata,'')
### some test code
data= """\
<COLLECTION VERSION="1">
<GENERAL PATH="S:\\" />
<ITEM NAME="Item1">
<DESCRIPTION>Item 1 description.</DESCRIPTION>
<DATE>Thu Dec 16 16:35:05 1999</DATE>
</ITEM>
<ITEM NAME="Item2">
<DESCRIPTION>Item 2 description.</DESCRIPTION>
<DATE>Wed Mar 15 11:54:27 2000</DATE>
</ITEM>
</COLLECTION>
"""
if __name__ == "__main__":
p=myparser()
p.feed(data)
p.close()
More information about the Python-list
mailing list