2016-09-13 30 views
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 

這什麼也不輸出,我想不通爲什麼代碼不會通過依附關係卷標迭代。任何幫助,將不勝感激。

回答

0

您的XML有一個小錯誤。應該有一個結束標記</project>,你可能會錯過這個問題。

對我來說,以下工作:

from xml.etree import ElementTree 
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.find(namespace+'groupId').text 
    artifactId = dependency.find(namespace+'artifactId').text 
    version = dependency.find(namespace+'version').text 
    print groupId, artifactId, version 

$ python -i a.py 
org.postgresql postgresql 9.4-1200-jdbc41 
com.amirsys referencedb 5.0.0-1 

你的.get()用法是錯誤的。請參閱.get()如何工作。比方說,你的XML是:

<?xml version="1.0"?> 
<data> 
    <country name="Liechtenstein"> 
     <rank>1</rank> 
     <year>2008</year> 
     <gdppc>141100</gdppc> 
     <neighbor name="Austria" direction="E"/> 
     <neighbor name="Switzerland" direction="W"/> 
    </country> 
    <country name="Singapore"> 
     <rank>4</rank> 
     <year>2011</year> 
     <gdppc>59900</gdppc> 
     <neighbor name="Malaysia" direction="N"/> 
    </country> 
    <country name="Panama"> 
     <rank>68</rank> 
     <year>2011</year> 
     <gdppc>13600</gdppc> 
     <neighbor name="Costa Rica" direction="W"/> 
     <neighbor name="Colombia" direction="E"/> 
    </country> 
</data> 

你編寫Python代碼,如:

import xml.etree.ElementTree as ET 
tree = ET.parse('country_data.xml') 
root = tree.getroot() 
for country in root.findall('country'): 
    rank = country.find('rank').text 
    name = country.get('name') 
    print rank, name 

這將打印:

Liechtenstein 1 
Singapore 4 
Panama 68 

正如你所看到的,.get()給你屬性的值。 docs對此很清楚。

+0

@ d.griner您應該閱讀:https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces –

+0

謝謝,這正好回答了我的問題。我很感激! –