1

所以我正在开发一个基于浏览器的程序,用 Python 编写,它从目录中的多个文件解析 XML 数据,然后返回页面上某些 XML 标记的值。我已经成功地能够从其中一个 XML 文件中返回值,但我希望从目录中的每个文件中收集数据并以电子表格格式返回值。如何解析每个 XML 文件中的数据?此外,XML 文件不是静态的,会有新文件来来去去。谢谢!下面是我的代码:

from xml.dom.minidom import parseString

import os
path = 'C:\Vestigo\XML'
listing = os.listdir(path)
for infile in listing:
    print infile

file = open(os.path.join(path,infile),'r')

data = file.read()
file.close()

dom = parseString(data)

xmlTag0 = dom.getElementsByTagName('Extrinsic')[0].toxml()
xmlData0 = xmlTag0.replace('<Extrinsic>','').replace('</Extrinsic>','')
xmlTag1 = dom.getElementsByTagName('DeliverTo')[0].toxml()
xmlData1 = xmlTag1.replace('<DeliverTo>','').replace('</DeliverTo>','')
xmlTag2 = dom.getElementsByTagName('Street1')[0].toxml()
xmlData2 = xmlTag2.replace('<Street1>','').replace('</Street1>','')
xmlTag3 = dom.getElementsByTagName('City')[0].toxml()
xmlData3 = xmlTag3.replace('<City>','').replace('</City>','')
xmlTag4 = dom.getElementsByTagName('State')[0].toxml()
xmlData4 = xmlTag4.replace('<State>','').replace('</State>','')
xmlTag5 = dom.getElementsByTagName('PostalCode')[0].toxml()
xmlData5 = xmlTag5.replace('<PostalCode>','').replace('</PostalCode>','')


import cherrypy
class Root(object):
    def index(self):
        return ('Order Number:', ' ', xmlData0, '<br>Name: ', xmlData1, '<br>Street   Address: ', xmlData2, '<br>City/State/Zip: ', xmlData3, ', ', xmlData4, ' ', xmlData5, ' ', """<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()

编辑:用我下面的解决方案更新。

4

1 回答 1

0

为了从目录中的每个文件中提取数据,我使用了以下代码:

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

感谢大家的帮助,以及回答我自己的问题希娜的提示!

于 2012-10-22T21:25:01.687 回答