Home >Backend Development >Python Tutorial >Python解析xml中dom元素的方法

Python解析xml中dom元素的方法

WBOY
WBOYOriginal
2016-06-10 15:17:271210browse

本文实例讲述了Python解析xml中dom元素的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
from xml.dom import minidom
try:
    xmlfile = open("path.xml", "a+")
    #xmldoc = minidom.parse( sys.argv[1])
    xmldoc = minidom.parse(xmlfile)
except :
    #updatelogger.error( "Can't parse Xml File." )
    sys.exit(0)
 
ClientOutputPath = xmldoc.getElementsByTagName('D')[0].attributes['path'].value
OutputPath = xmldoc.getElementsByTagName('h')[0].attributes['path'].value
BasePath = xmldoc.getElementsByTagName('th')[0].attributes['path'].value
ToolPath = xmldoc.getElementsByTagName('ub')[0].attributes['path'].value
ToolPath_2 = xmldoc.getElementsByTagName('ub')[1].attributes['path'].value
 
ClientOutputPath.replace( "\\", "\\\\" )
OutputPath.replace( "\\", "\\\\" )
BasePath.replace( "\\", "\\\\" )
ToolPath.replace( "\\", "\\\\" )
 
print ClientOutputPath
print OutputPath
print BasePath
print ToolPath
print ToolPath_2

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn