python读取自定义xml文件
时间:2021-06-07 00:28:10
收藏:0
阅读:28
一、定义xml文件内容,既然是自定义则所有的根节点随便写
使用xml.dom.minidom三方模块对xml文件进行解析
from xml.dom.minidom import parse
def getIP(machineNum):
#读取xml文件
domTree = parse("./config/%s.xml" % (machineNum[0]))
#文档根元素
rootNode = domTree.documentElement
print(rootNode.nodeName)
#所有的机台
machines = rootNode.getElementsByTagName(‘machine‘)
print("---------------------")
for machine in machines:
# Num 元素
num = machine.getElementsByTagName("num")[0]
print(num.nodeName, ":", num.childNodes[0].data)
# ip 元素
ip = machine.getElementsByTagName("ip")[0]
print(ip.nodeName, ":", ip.childNodes[0].data)
if __name__ == ‘__main__‘:
getIP(A)
运行结果
原文:https://www.cnblogs.com/dsyMoon/p/14856822.html
评论(0)