Search code examples
xmlpython-3.xloopsgraphml

Iterating through a GraphML (XML) file and extracting all the values


I have an XML (actually GraphML) file as follows:

<?xml version="1.0" ?>
<graphml>
    <key attr.name="label" attr.type="string" id="label"/>
    <graph edgedefault="directed" id="">
        <node id="0">
            <type>p</type>
            <label>sshd(3,2)</label>
        </node>
        <node id="1">
            <type>e</type>
            <label>user(3)</label>
        </node> 
    </graph>
</graphml>

I want to iterate through this and extract each individual item (print it). In a normal XML file, this is reasonably straight forward, however in this case I can't seem to be able to do this. Here is the code that I am using.

tree = ET.parse(FILENAME)
    root=tree.getroot()
    for child in root:
        for x in child:
            NodeType =  child.find('node').find('type')
            LabelType = child.find('node').find('label')
            print(NodeType.text, LabelType.text)

What this gives me are the first two values repeated twice, in other words:

p  sshd(3,2)
p  sshd(3,2)

instead of:

p  sshd(3,2)
e  user(3)

Can anybody help with this?


Solution

  • You can specify exact path from the root to node elements at once :

    for node in root.findall('graph/node'):
        print(node.find('type').text, node.find('label').text)
    

    eval.in demo