[XML-SIG] Fwd: Parse MULTIPLE XML files in a directory

amitesh kumar amitesh116 at gmail.com
Fri Aug 10 11:30:49 CEST 2007


---------- Forwarded message ----------
From: Stefan Behnel <stefan_ml at behnel.de>
Date: Aug 10, 2007 1:23 PM
Subject: Re: [XML-SIG] Parse MULTIPLE XML files in a directory
To: amitesh kumar <amitesh116 at gmail.com>
Cc: xml-sig at python.org

Hi,

first thing: don't use expat directly. Use (c)ElementTree's iterparse. It's
in
Python 2.5, but is also available as an external package for older Python
versions. There's also lxml (which is mostly compatible to ElementTree), in
case you ever need features like XPath, XSLT or whatever.


amitesh kumar wrote:
> Please review the following code and help me.
>
> Here I'm trying to :
> 1. Read each XML file in a folder.
> 2. Parse file.
> 3. Store some of the tags values as key-value pair in a map
> 4. Similarly maintain another collection that'll store one list per file.
> ------------------------------------------------------------------------
>
> ordtags = set()
> shptags = set()
> omptags = set()
>
> ordtags.add('orrfnbr')
> ordtags.add('afidlog')
[...]

Better:

    ordtags = set(['offfnbr', 'afidlog', ...])

    from xml.etree.cElementTree import iterparse

    for onefile in allfiles:
        for event, element in iterparse(onefile):
            if element.tag in ordtags:
                 # do something like
                 values[element.tag] = element.text
            elif element.tag in shptags:
                 # do something else
            else:
                 # don't do anything?
            element.clear()

Stefan


-- 
With Regards
Amitesh K.
9850638640
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/xml-sig/attachments/20070810/f8b354e7/attachment.html 


More information about the XML-SIG mailing list