python 解析xml

find 语法为:{命名空间}标签名, 用 ‘’/‘分割搜索路径, ‘./ ’ 表示当前节点路径, 支持多级路径搜索,下面就是个两级路径搜索的例子,搜索路径用’/’ 分开即可。
注意find只能查找一个node, 要想获得所有搜索结果可以用findAll()

from xml.etree import ElementTree 
tree = _ElementTree.parse(path)
root = tree.getroot()
node = root.find("./{http://maven.apache.org/POM/4.0.0}properties/{http://maven.apache.org/POM/4.0.0}hadoop.version")
print(node.text) # 2.5.0-cdh5.2.0


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.aac.dmpgroupId>
    <artifactId>aac-extractorsartifactId>
    <version>1.0.0version>

    <properties>
        <xray.version>2.0.0xray.version>
        <zampdmp.version>1.1.0zampdmp.version>
        <zampdmp.record.version>1.0.0zampdmp.record.version>
        <hadoop.version>2.5.0-cdh5.2.0hadoop.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    
project>

你可能感兴趣的:(python,随笔,python,xml,开发语言)