使用Python访问XML元素值

问题描述 投票:0回答:1

我是Python的新手,所以我很抱歉,如果以前已经介绍过这个问题并且我一直无知应用解决方案。

这是XML:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>DEFAULT</groupId>
  <artifactId>ADP_ServiceTechnology-JRG_Testing</artifactId>
  <version>2.0.31</version>
  <dependencies>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>ADP Standard Operations</artifactId>
      <version>2.2.86.17-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>Base</artifactId>
      <version>1.9.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>Databases</artifactId>
      <version>[1.1.0]</version>
    </dependency>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>HPE Solutions</artifactId>
      <version>[1.8.2]</version>
    </dependency>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>Business Applications</artifactId>
      <version>[1.3.0]</version>
    </dependency>
    <dependency>
      <groupId>DEFAULT</groupId>
      <artifactId>Operating Systems</artifactId>
      <version>[1.3.0]</version>
    </dependency>
  </dependencies>
</project>

我成功导入数据:

import xml.etree.ElementTree as ET
tree = ET.parse('pom.xml')
root = tree.getroot()

我只需要遍历树并检索<artifactId><version>值。我已尝试在网上找到许多方法,没有运气。使用php和xpath对我来说很简单,但是python让我感到难过。

这个:

for elem in tree.iter():
  print "%s: '%s'" % (elem.tag, elem.text)  

将返回每个元素标签和文本,但我想导航到我指出的两个。

提前致谢!

python xml parsing
1个回答
0
投票

如果您在命名空间前添加,则可以找到它们:

import xml.etree.ElementTree as ET
tree = ET.parse("t.xml")
root = tree.getroot()

namesp = root.tag.replace("project","")  # get the namesapce from the root key

version = root.find(namesp+"version")
artifactId = root.find(namesp+"artifactId")

print(version.text)
print(artifactId.text)

输出:

2.0.31
ADP_ServiceTechnology-JRG_Testing 

您可以在这里找到更多一般信息:Parsing XML in Python using ElementTree example

并在doku https://docs.python.org/3/library/xml.etree.elementtree.html(切换到页面顶部的python 2)

如果要从数据中删除命名空间,请参阅https://stackoverflow.com/a/25920989/7505395中的Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"

© www.soinside.com 2019 - 2024. All rights reserved.