Cherrypy and Parsing XML Data from multiple files

那年仲夏 提交于 2019-12-11 15:23:35

问题


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()

回答1:


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>""")


来源:https://stackoverflow.com/questions/13018799/cherrypy-and-parsing-xml-data-from-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!