Search code examples
pythonxmlxml-parsingcherrypy

Cherrypy and Parsing XML Data from multiple files


So this is sort of a piggy-back post of another question I had. I've successfully pulled data from multiple xml files and am able to get the data to display within the terminal using the print function, but when I try to use the return function to show the data in the browser, I only get the data from the first file. Any ideas on why I only get data from the first file rather than all of them? Thanks!

from xml.dom.minidom import parse, parseString
import os, glob, re
import cherrypy
class Root(object):
    def index(self):
        path = 'C:\Vestigo\XML'

        TOTALXML = len(glob.glob(os.path.join(path, '*.xml')))
        print TOTALXML
        i = 0

        for XMLFile in glob.glob(os.path.join(path, '*.xml')):
            xmldoc = parse(XMLFile)
            order_number = xmldoc.getElementsByTagName('Extrinsic')[0].firstChild.data
            order_name = xmldoc.getElementsByTagName('DeliverTo')[0].firstChild.data
            street1 = xmldoc.getElementsByTagName('Street1')[0].firstChild.data
            state = xmldoc.getElementsByTagName('State')[0].firstChild.data
            zip_code = xmldoc.getElementsByTagName('PostalCode')[0].firstChild.data
            OUTPUTi = order_number+' '+order_name+' '+street1+' '+state+' '+zip_code
            i += 1
            print OUTPUTi
        return (OUTPUTi, """<br><br><a href="/exit">Quit</a>""")
    index.exposed = True

    def exit(self):
        raise SystemExit(0)
    exit.exposed = True

def start():
    import webbrowser
    cherrypy.tree.mount(Root(), '/')
    cherrypy.engine.start_with_callback(
        webbrowser.open,
        ('http://localhost:8080/',),
        )
    cherrypy.engine.block()

if __name__=='__main__':
    start()

Solution

  • You are not collecting the data anywhere; you store everything in a variable named OUTPUTi, then only return the last iteration of that variable. Python does not magically make that variable use the i counter.

    Use a list to collect the strings:

        TOTALXML = len(glob.glob(os.path.join(path, '*.xml')))
        print TOTALXML
        OUTPUT = []
    
        for XMLFile in glob.glob(os.path.join(path, '*.xml')):
            xmldoc = parse(XMLFile)
            order_number = xmldoc.getElementsByTagName('Extrinsic')[0].firstChild.data
            order_name = xmldoc.getElementsByTagName('DeliverTo')[0].firstChild.data
            street1 = xmldoc.getElementsByTagName('Street1')[0].firstChild.data
            state = xmldoc.getElementsByTagName('State')[0].firstChild.data
            zip_code = xmldoc.getElementsByTagName('PostalCode')[0].firstChild.data
            OUTPUT.append(order_number+' '+order_name+' '+street1+' '+state+' '+zip_code)
            print OUTPUT[-1]
    
        OUTPUT = ''.join(OUTPUT)
        return (OUTPUT, """<br><br><a href="/exit">Quit</a>""")