使用 ElementTree 混淆根元素
Root element confusion using ElementTree
我在 Python 3.5.1 中使用 ElementTree。我想像这样解析 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<name>A name</name>
<groupId>a.group</groupId>
<artifactId>anArtifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<dependency-version>10.0</dependency-version>
<another-dependency-version>11.0</another-dependency-version>
</properties>
</project>
并获取标签dependency-version的值。我开始尝试使用以下代码获取 properties:
mydoc = ElementTree.parse(sources + "pom.xml")
root = mydoc.getroot()
for element in root.findall('properties'):
print(element)
问题是除了根标签 project 及其属性,我什么都没有。
>>> root.tag
'{http://maven.apache.org/POM/4.0.0}project'
>>> root.text
'\n '
>>> root.attrib
{'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'}
我也直接用 mydoc 试过:
>>> root.findall('project')
[]
>>> mydoc.findall('./properties')
[]
>>> mydoc.findall('./project/properties')
[]
我知道 getroot() 会给我项目标签,我可以从那里开始工作,但我好像出错了。
编辑
我遵循了建议的解决方案并得到了:
>>> ns
{'sm': 'http://maven.apache.org/POM/4.0.0'}
>>> mydoc.findall('.//sm:properties', ns)
[<Element '{http://maven.apache.org/POM/4.0.0}properties' at 0x0325AA80>]
>>> root.findall('.//sm:properties', ns)
[<Element '{http://maven.apache.org/POM/4.0.0}properties' at 0x0325AA80>]
>>> mydoc.findall('.//sm:properties/dependency-version', ns)
[]
现在好像在找东西,但是没有找到标签的两个元素 properties
最后我的想法来自:Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"
什么是基本上摆脱命名空间。
import re
import xml.etree.ElementTree as ElementTree
filestring = open("C:/temp/test.xml", "r").read()
xmlwithoutns = re.sub('<project[^>]+', '<project>', filestring, count=1)
tree = ElementTree.fromstring(xmlwithoutns)
value = tree.findall("properties/dependency-version")[0].text
我在 Python 3.5.1 中使用 ElementTree。我想像这样解析 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<name>A name</name>
<groupId>a.group</groupId>
<artifactId>anArtifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<dependency-version>10.0</dependency-version>
<another-dependency-version>11.0</another-dependency-version>
</properties>
</project>
并获取标签dependency-version的值。我开始尝试使用以下代码获取 properties:
mydoc = ElementTree.parse(sources + "pom.xml")
root = mydoc.getroot()
for element in root.findall('properties'):
print(element)
问题是除了根标签 project 及其属性,我什么都没有。
>>> root.tag
'{http://maven.apache.org/POM/4.0.0}project'
>>> root.text
'\n '
>>> root.attrib
{'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'}
我也直接用 mydoc 试过:
>>> root.findall('project')
[]
>>> mydoc.findall('./properties')
[]
>>> mydoc.findall('./project/properties')
[]
我知道 getroot() 会给我项目标签,我可以从那里开始工作,但我好像出错了。
编辑
我遵循了建议的解决方案并得到了:
>>> ns
{'sm': 'http://maven.apache.org/POM/4.0.0'}
>>> mydoc.findall('.//sm:properties', ns)
[<Element '{http://maven.apache.org/POM/4.0.0}properties' at 0x0325AA80>]
>>> root.findall('.//sm:properties', ns)
[<Element '{http://maven.apache.org/POM/4.0.0}properties' at 0x0325AA80>]
>>> mydoc.findall('.//sm:properties/dependency-version', ns)
[]
现在好像在找东西,但是没有找到标签的两个元素 properties
最后我的想法来自:Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method "find", "findall" 什么是基本上摆脱命名空间。
import re
import xml.etree.ElementTree as ElementTree
filestring = open("C:/temp/test.xml", "r").read()
xmlwithoutns = re.sub('<project[^>]+', '<project>', filestring, count=1)
tree = ElementTree.fromstring(xmlwithoutns)
value = tree.findall("properties/dependency-version")[0].text