daydreamer
daydreamer

Reputation: 92149

python : how to parse zipped xml file?

My code looks like

def read_zip_file():
    import zipfile
    zf = zipfile.ZipFile(os.path.expanduser('~/Downloads/tmp/me.zip'))
    for filename in [ 'myfile.xml' ]:
        print filename
        try :
            data = get_proposal_data_map(zf.read(filename))
            print data
        except:
            logging.error('error - ' + str(sys.exc_info()))

This spits out the xml as regular file. Now I have a existing code, which given a path parses XML as

try:
    tree = etree.parse(path)
    root = tree.getroot()
    for child in root:
              # do things with XML

Question

How can I parse a zipped XML (myfile.xml.zip) as regular XML file?

Upvotes: 0

Views: 4728

Answers (1)

Isaac
Isaac

Reputation: 3616

You can just read the zip file into a variable and then use

root = etree.fromstring(xmlstr)

Upvotes: 2

Related Questions