Etree returns a "random" string instead of attribute name
<p>I am new to python and trees at all, and have encountered some problems.</p>

<p>I have the following dataset structured as:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;graphml xmlns="http://graphml.graphdrawing.org/xmlns"&gt;
  &lt;node id="someNode"&gt;
    &lt;data key="label"&gt;someNode&lt;/data&gt;
  &lt;/node&gt;
&lt;/graphml&gt;
</code></pre>

<p>I want to reach the attribute and attribute values for both the root and the node elements.</p>

<p>I have tried using Python xml.etree.ElementTree like this:</p>

<pre><code>import xml.etree.ElementTree as etree

tree = etree.parse('myDataset')
root = tree.getroot()

print('Root: ', root)

print('Children: ', root.getchildren())
</code></pre>

<p>but this is what I get:</p>

<pre><code>Root:  &lt;Element '{http://graphml.graphdrawing.org/xmlns}graphml' at 0x031DB5A0&gt;
Children:  [&lt;Element '{http://graphml.graphdrawing.org/xmlns}key' at 0x03F9BFC0&gt;
</code></pre>

<p>I did also try .text and .tag, which only removed the "at 0x03...".</p>

<p>Hope the question is understandable and someone know a solution.</p>

<p>If you want to output your root and children nodes as xml text, instead of the default representation, use <code>xml.etree.ElementTree.tostring(root)</code> and</p>

<pre><code>for child in root:
    xml.etree.ElementTree.tostring(child)
</code></pre>

<p>official doc here: <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring" rel="nofollow">https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring</a></p>

<p>And if you want he tag name, use the <code>tag</code> property of each element:</p>

<pre><code>print(root.tag)
for child in root:
    print(child.tag)
</code></pre>

<p>doc describing the available attributes: <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element" rel="nofollow">https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element</a></p>

python xml tree elementtree graphml