解析XML python
#!/usr/bin/env python
import os
import sys
import time
import re
#import xml.dom.minidom.Document
import xml
from xml.dom.minidom import Document
class ConfigHandler:
flagadd = False
flag = False
key = ""
childtuple = []
roottuple = []
def doc_order_iter(self, node):
yield node
for child in node.childNodes:
for cn in self.doc_order_iter(child):
yield cn
return
def pasernodecfg(self, node):
self.flag = False
self.flagadd = False
def pasernodefileName(self, node):
#print dir(node)
self.key = node.nodeName
self.flag = True
def pasernodemodify(self, node):
self.key = node.nodeName
self.flag = True
def pasernodedict(self, node):
self.key = node.nodeName
self.flag = True
self.flagadd = True
def addValue(self, node):
print self.key + "=" + node.nodeValue
self.childtuple.append(node.nodeValue)
if (self.flagadd == True):
print "add++++"
self.roottuple.append(self.childtuple)
self.childtuple = []
def getTasksfromxml(self, asConfigFilename):
#lsContent = open(asConfigFilename).read()
f = open(asConfigFilename)
s = f.read()
f.close()
doc = xml.dom.minidom.parseString(s)
for node in self.doc_order_iter(doc):
if self.flag == True:
self.addValue(node)
if node.nodeName == "cfg":
self.pasernodecfg(node)
elif node.nodeName == "ConfigurationFilename":
self.pasernodefileName(node)
elif node.nodeName == "ModifyMethod":
self.pasernodemodify(node)
elif node.nodeName == "ToBeModifiedSecion":
self.pasernodedict(node)
else:
self.flag = False
return self.roottuple
if __name__ == "__main__":
import sys
if len(sys.argv)