1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
from xml.dom.minidom import parse import xml.dom.minidom
DOMTree = xml.dom.minidom.parse("test.xml") collection = DOMTree.documentElement
trans = {'Number': 'NUMBER', 'Signal': 'SIGNAL', 'Background': 'BACKGROUND'} resultDict = {}
for cycle in xrange(1, 3): cycleData = collection.getElementsByTagName("Cycle%d" % cycle) if not cycleData: continue else: for k in trans: value = cycleData[0].getElementsByTagName(k)[0] value = value.childNodes[0].data value = value.strip().split()
resultDict.setdefault(trans[k], []).extend(map(float, value))
for k in resultDict: print k, resultDict[k]
tigerose@pc ~/github/parseXml $python parseDOM.py SIGNAL [15168.389648, 19429.083984, 24276.886719, 18786.134766, 10683.573242, 14735.889648, 19846.058594, 13917.609375] NUMBER [628398.0, 482765.0] BACKGROUND [-739.025574, -691.423401, -794.166931, -1007.662659, -445.148132, -482.349854, -625.839417, -890.880981]
|