作者:手机用户2602936275 | 来源:互联网 | 2023-05-19 13:17
演示如何用Python解析XML。
如下分别是待解析的XML报文,尾部是Python 源码。
-
- xml version="1.0" encoding="iso8859-1"?>
- <view id="CCBSA_v" msgType="1">
- <viewElem id="jrcfwq" dispName="集群">
- <viewElem id="fwqsl" dispName="服务器数量" value="1"/>
- <viewElem id="Master" dispName="主控节点" value="STAR"/>
- <viewElem id="Server" >
- <viewElem id="SrvName" value="01" dispName="STAR">
- <viewElem id="Plat" dispName="平台信息">
- <viewElem id="PlatStat" dispName="平台状态">
- <viewElem id="MchPort" dispName="机器端口" value="12000"/>
- <viewElem id="MchState" dispName="机器状态" value="在线"/>
- <viewElem id="AllProc" dispName="总进程数" value="0"/>
- <viewElem id="EslProc" dispName="平台进程数" value="29"/>
- <viewElem id="SemNum" dispName="信号灯数" value="104"/>
- <viewElem id="DrqSem" dispName="DRQ使用信号灯" value="53"/>
- viewElem>
- <viewElem id="CpuStat" dispName="CPU状态">
- <viewElem id="CpuIdle" dispName="idle" value="0"/>
- <viewElem id="CpuUser" dispName="user" value="0"/>
- <viewElem id="CpuNice" dispName="nice" value="0"/>
- <viewElem id="CpuSys" dispName="sys" value="0"/>
- <viewElem id="CpuWio" dispName="wio" value="0"/>
- viewElem>
- <viewElem id="SysInfo" dispName="操作系统信息">
- <viewElem id="CpuNum" dispName="CPU颗数" value="0"/>
- <viewElem id="PhyMem" dispName="物理内存(K)" value="0"/>
- <viewElem id="VirtMem" dispName="虚拟内存(K)" value="0"/>
- <viewElem id="UsedMem" dispName="已用内存(K)" value="0"/>
- <viewElem id="exit_freeMem" dispName="可用内存(K)" value="0"/>
- <viewElem id="IFaceNum" dispName="网络接口数" value="0"/>
- viewElem>
- <viewElem id="ProjInfo" dispName="项目信息">
- <viewElem id="ShmSize" dispName="部署共享内存大小(k)" value="15251168"/>
- <viewElem id="ShmKeep" dispName="部署共享内存剩余(k)" value="16769072"/>
- <viewElem id="BcbSize" dispName="交换共享内存大小(k)" value="6304"/>
- <viewElem id="BcbUsed" dispName="已用交换共享内存(k)" value="2208"/>
- <viewElem id="BcbMax" dispName="最大交换共享内存(k)" value="70816"/>
- <viewElem id="ChkNum" dispName="当前已用缓冲区段数" value="1"/>
- <viewElem id="DrqMsg" dispName="缓存消息数" value="0"/>
- <viewElem id="MsqNum" dispName="队列中的消息数" value="0"/>
- <viewElem id="TranNum" dispName="本机交易统计" value="322047"/>
- <viewElem id="DTAMPID" dispName="DTA管理服务进程号" value="3727372"/>
- <viewElem id="RCYCLPID" dispName="垃圾回收服务进程号" value="3739830"/>
- <viewElem id="SYNCPID" dispName="多机同步服务进程号" value="3670122"/>
- <viewElem id="ISSUEPID" dispName="多机发布服务进程号" value="2871348"/>
- <viewElem id="MACHSVRPID" dispName="多机交换服务进程号" value="0"/>
- <viewElem id="MONCPID" dispName="多机监控同步服务进程号" value="0"/>
- <viewElem id="MONSPID" dispName="多机监控发布服务进程号" value="0"/>
- <viewElem id="DTANUM" dispName="适配器个数" value="12"/>
- ....
- ....
- ......
- from xml.dom import minidom
- from sys import stderr
- from os.path import join
- __version__ = "V1.1"
- dataTypMap = {"integer": 0, "foat": 1, "boolean": 2,
- "char": 3, "string": 4, "void": 5, "double": 6,
- "long": 7, "object": 8, "datetime": 9}
- nodeTypMap = {"LEAF": 0, "NODE": 2}
- nodeImgMap = {nodeTypMap["LEAF"]: "/images/leaf.gif",
- nodeTypMap["NODE"]: "/images/node.gif"}
- whoMap = {"Views": 0, "Nodes": 1,
- "NodeViews": 2, "NodesRel": 3}
- class TIndicatorData:
- def __init__ (self, XML, dest):
- self.__xmldoc = minidom.parse (XML.strip (" "))
- self.__root = self.__xmldoc.documentElement
- self.__destPath = dest.strip (" ")
- self.__gNodesList = []
- self.__hViews = None
- self.__hNodes = None
- self.__hNodeViews = None
- self.__hNodesRel = None
- self.__outputData = {"Views": "", "Nodes": "", "NodeViews": "", "NodesRel": ""}
-
- def GenIndicators (self):
- self.__hViews = open (join (self.__destPath, "isac_mnt_view.txt"), "w+")
- self.__hNodes = open (join (self.__destPath, "isac_mnt_basenode.txt"), "w+")
- self.__hNodeViews = open (join (self.__destPath, "isac_mnt_basenode_view.txt"), "w+")
- self.__hNodesRel = open (join (self.__destPath, "isac_mnt_basenode_rel.txt"), "w+")
- try:
- self.__retrieveIndViewData ("Nodes")
- self.__retrieveIndViewData ("Views")
- self.__retrieveIndViewData ("NodeViews")
- self.__retrieveIndViewData ("NodesRel")
- self.__write (self.__hNodes, "Nodes")
- self.__write (self.__hViews, "Views")
- self.__write (self.__hNodeViews, "NodeViews")
- self.__write (self.__hNodesRel, "NodesRel")
- finally:
- self.__hViews.close ()
- self.__hNodes.close ()
- self.__hNodeViews.close ()
- self.__hNodesRel.close ()
-
- def __write (self, fileHandle, outputDataName):
- if not fileHandle:
- stderr.write ("Invalid file handle corresponding to %s" % outputDataName)
- return
- fileHandle.write (self.__outputData[outputDataName.strip (" ")].encode ("GB2312"))
- print fileHandle.name
- def __retrieveIndViewData (self, whoMapKeyName):
- self.__getNodes (self.__root, whoMap[whoMapKeyName])
- self.__clearNodesList ()
- def __clearNodesList (self):
- self.__gNodesList = []
- def __getNodePaths (self, ANode, isDynNode = True, initPath = ""):
-
- if (not ANode) or (not ANode.hasChildNodes ()):
- return initPath
- if initPath.strip (" ") == "":
- initPath = "/%s" % ANode.attributes["id"].value.strip (" ")
-
- for elem in ANode.childNodes:
- if (not elem.localName) or (not elem.hasChildNodes ()):
- continue
-
- idPath = ""
- if isDynNode and elem.hasAttribute ("value"):
- idPath = "%s/%s" % (initPath, elem.attributes["value"].value.strip (" "))
- else:
- idPath = "%s/%s" % (initPath, elem.attributes["id"].value.strip (" "))
- print idPath
- self.__getNodePaths (elem, isDynNode, idPath)
-
-
- def __getNodeType (self, ANode):
- ret = nodeTypMap["LEAF"]
- if ANode.hasChildNodes ():
- ret = nodeTypMap["NODE"]
- return ret
-
- def __outputNodes (self, ANode):
-
- if not ANode:
- return
-
- nodeType = self.__getNodeType (ANode)
- dataType = dataTypMap["void"]
- if nodeType == nodeTypMap["LEAF"] and ANode.hasAttribute ("value"):
- dataValue = ANode.attributes["value"].value.strip (" ")
- if dataValue.isdigit ():
- dataType = dataTypMap["integer"]
- else:
- try:
- float (dataValue)
- dataType = dataTypMap["float"]
- except:
- dataType = dataTypMap["string"]
- dispName = ANode.attributes["id"].value.strip (" ")
- if ANode.hasAttribute ("dispName"):
- dispName = ANode.attributes["dispName"].value.strip (" ") /
- .encode ("ISO8859").decode ("GB2312")
- ProcType = 0
- PrstType = 1
- UnitName = ' '
- initValue = '0'
- updateTime = ' '
- self.__outputData["Nodes"] += "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|/n" % /
- (ANode.attributes["id"].value.strip (" "),
- dispName, dispName, nodeImgMap[nodeType],
- nodeType, dataType, ProcType, PrstType,
- UnitName, initValue, updateTime)
-
- def __outputViews (self, ANode):
-
- if (not ANode) or (self.__getNodeType (ANode) != nodeTypMap["NODE"]):
- return
- nodeID = ANode.attributes["id"].value.strip (" ")
- viewName = nodeID
- if ANode.hasAttribute ("dispName"):
- viewName = ANode.attributes["dispName"].value /
- .encode ("ISO8859").decode ("GB2312")
-
- self.__outputData["Views"] += "%s|%s|%s|/n" % (nodeID, viewName, nodeID)
-
-
- def __outputNodeViews (self, ANode):
- if not ANode:
- return
-
- if self.__getNodeType (ANode) == nodeTypMap["NODE"]:
- self.__outputData["NodeViews"] += /
- "%s|%s|/n" % /
- (ANode.attributes["id"].value.strip (" "),
- ANode.attributes["id"].value.strip (" "))
- else:
- self.__outputData["NodeViews"] += /
- "%s|%s|/n" % /
- (ANode.attributes["id"].value.strip (" "),
- ANode.parentNode.attributes["id"].value.strip (" "))
-
- def __outputNodesRel (self, ANode):
- if (not ANode) or (ANode.parentNode.nodeType == ANode.DOCUMENT_NODE):
- return
- self.__outputData["NodesRel"] += "%s|%s|/n" % /
- (ANode.parentNode.attributes["id"].value.strip (" "),
- ANode.attributes["id"].value.strip (" "))
-
- def __handleByType (self, ANode, Who):
-
- if not ANode:
- return
-
- if Who == whoMap["Nodes"]:
- self.__outputNodes (ANode)
- elif Who == whoMap["Views"]:
- self.__outputViews (ANode)
- elif Who == whoMap["NodeViews"]:
- self.__outputNodeViews (ANode)
- elif Who == whoMap["NodesRel"]:
- self.__outputNodesRel (ANode)
-
- def __getNodes (self, ANode, Who):
-
- if not ANode:
- return
-
- if ANode.parentNode.nodeType == ANode.DOCUMENT_NODE:
- self.__handleByType (ANode, Who)
- for elem in ANode.childNodes:
- if not elem.localName:
- continue
- if not elem.hasAttribute ("id"):
- stderr.write ("Invalid tag without an attribute named 'id' FOUND!")
- continue
- tagID = elem.attributes["id"].value.strip (" ")
- try:
- self.__gNodesList.index (tagID)
- continue
- except ValueError:
-
- self.__gNodesList.append (tagID)
- self.__handleByType (elem, Who)
- self.__getNodes (elem, Who)
-
- if __name__ == "__main__":
-
- ind = TIndicatorData (
- r"D:/Documents/Construction_Bank/SH/CCBSA/Design/ccbsa_ind21.xml",
- r"D:/Temp")
- ind.GenIndicators ()
- print "/nMISSION COMPLETED."