Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn XML Values from Multiple Files in Python?
    primarykey
    data
    text
    <p>So I am working on a browser based program, written in Python, that parses XML data from multiple files in a directory, then returns the values of certain XML tags on the page. I have successfully been able to return the values from one of the XML files, but am hoping to collect data from every file within the directory and return the values in spreadsheet format. How do I parse the data from every XML file? Also, the XML files are not static, there will be new files coming and going. Thanks! Below is my code:</p> <pre><code>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('&lt;Extrinsic&gt;','').replace('&lt;/Extrinsic&gt;','') xmlTag1 = dom.getElementsByTagName('DeliverTo')[0].toxml() xmlData1 = xmlTag1.replace('&lt;DeliverTo&gt;','').replace('&lt;/DeliverTo&gt;','') xmlTag2 = dom.getElementsByTagName('Street1')[0].toxml() xmlData2 = xmlTag2.replace('&lt;Street1&gt;','').replace('&lt;/Street1&gt;','') xmlTag3 = dom.getElementsByTagName('City')[0].toxml() xmlData3 = xmlTag3.replace('&lt;City&gt;','').replace('&lt;/City&gt;','') xmlTag4 = dom.getElementsByTagName('State')[0].toxml() xmlData4 = xmlTag4.replace('&lt;State&gt;','').replace('&lt;/State&gt;','') xmlTag5 = dom.getElementsByTagName('PostalCode')[0].toxml() xmlData5 = xmlTag5.replace('&lt;PostalCode&gt;','').replace('&lt;/PostalCode&gt;','') import cherrypy class Root(object): def index(self): return ('Order Number:', ' ', xmlData0, '&lt;br&gt;Name: ', xmlData1, '&lt;br&gt;Street Address: ', xmlData2, '&lt;br&gt;City/State/Zip: ', xmlData3, ', ', xmlData4, ' ', xmlData5, ' ', """&lt;br&gt;&lt;br&gt;&lt;a href="/exit"&gt;Quit&lt;/a&gt;""") 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() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload