0
我有一個像XML POM文件從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>
<parent>
<groupId>com.amirsys</groupId>
<artifactId>components-parent</artifactId>
<version>RELEASE</version>
</parent>
<artifactId>statdxws</artifactId>
<version>6.5.0-16</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amirsys</groupId>
<artifactId>referencedb</artifactId>
<version>5.0.0-1</version>
<exclusions>
<exclusion>
<groupId>com.amirsys</groupId>
<artifactId>jig</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
我想拉用元素樹創建的組id,artifactIds和版本一個依賴項對象,但它不會找到依賴項標籤。這是到目前爲止我的代碼:
tree = ElementTree.parse('pomFile.xml')
root = tree.getroot()
namespace = '{http://maven.apache.org/POM/4.0.0}'
for dependency in root.iter(namespace+'dependency'):
groupId = dependency.get('groupId')
artifactId = dependency.get('artifactId')
version = dependency.get('version')
print groupId, artifactId, version
這什麼也不輸出,我想不通爲什麼代碼不會通過依附關係卷標迭代。任何幫助,將不勝感激。
@ d.griner您應該閱讀:https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces –
謝謝,這正好回答了我的問題。我很感激! –