Getting child attributes from an XML document using element tree
<p>I have an xml pom file like the following:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
    &lt;groupId&gt;com.amirsys&lt;/groupId&gt;
    &lt;artifactId&gt;components-parent&lt;/artifactId&gt;
    &lt;version&gt;RELEASE&lt;/version&gt;
&lt;/parent&gt;
&lt;artifactId&gt;statdxws&lt;/artifactId&gt;
&lt;version&gt;6.5.0-16&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
        &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
        &lt;version&gt;9.4-1200-jdbc41&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
                &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.amirsys&lt;/groupId&gt;
        &lt;artifactId&gt;referencedb&lt;/artifactId&gt;
        &lt;version&gt;5.0.0-1&lt;/version&gt;
        &lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;com.amirsys&lt;/groupId&gt;
                &lt;artifactId&gt;jig&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
</code></pre>

<p></p>

<p>I am trying to pull the groupIds, artifactIds and versions using element tree to create a dependency object, but it won't find the dependency tags.  This is my code so far:</p>

<pre><code>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
</code></pre>

<p>This outputs nothing, and I can't figure out why the code isn't iterating through the dependency tag.  Any help would be appreciated.</p>

<p>Your XML has a small mistake. There should be a closing tag <code>&lt;/project&gt;</code> which you probably missed in the question.</p>

<p>The following works for me:</p>

<pre><code>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
</code></pre>

<p>Your usage of <code>.get()</code> is wrong. See how <code>.get()</code> works. Let's say your xml is:</p>

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

<p>And you write python code like:</p>

<pre><code>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
</code></pre>

<p>This will print:</p>

<blockquote>
<pre><code>Liechtenstein 1
Singapore 4
Panama 68
</code></pre>
</blockquote>

<p>As you can see, <code>.get()</code> gives you the value of the attribute. The <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.get" rel="nofollow">docs</a> are pretty clear on this.</p>

python maven elementtree