Cherrypy和解析来自多个文件的XML数据 [英] Cherrypy and Parsing XML Data from multiple files

查看:62
本文介绍了Cherrypy和解析来自多个文件的XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这有点像是我曾提过的另一个问题.我已经成功地从多个xml文件中提取了数据,并且能够使用print函数将数据显示在终端中,但是当我尝试使用return函数在浏览器中显示数据时,我只能从第一个文件.关于为什么我仅从第一个文件而不是所有文件中获取数据的任何想法?谢谢!

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

推荐答案

您没有在任何地方收集数据.您将所有内容存储在名为 OUTPUTi 的变量中,然后仅返回该变量的最后一次迭代.Python不会神奇地使该变量使用 i 计数器.

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.

使用列表收集字符串:

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

这篇关于Cherrypy和解析来自多个文件的XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆