0

I have a big AML/XML file with many layers and child of child elements. I need to display the ElementTree tags as a Digraph. I only want to use lxml, networkx and matplotlib!

I've run into multiple questions / issues:

  1. How do I automatically iterate and loop through all the subtrees and child elements e.g. when i dont know how many layers i have? Is there a smarter way than making endless for loops?
  2. How do i format the digraph to look like a tree instead of a mess and how do i get rid of self-referencing edges?

Hope somebody can give me advice and a smarter solution Here's what I wrote and my result so far:

from lxml import etree
import networkx as nx
import matplotlib.pyplot as plt

G=nx.DiGraph()

tree = etree.parse("Test_AML.aml")
root = tree.getroot()
for node in root.getchildren():
    G.add_node(node.tag)
    G.add_edge(root.tag, node.tag)
    print(node.tag)
    for element in node.getchildren():
        G.add_node(element.tag)
        G.add_edge(node.tag, element.tag)
        for knot in element.getchildren():
            G.add_node(knot.tag)
            G.add_edge(element.tag, knot.tag)
            for leaf in knot.getchildren():
                G.add_node(leaf.tag)
                G.add_edge(knot.tag, leaf.tag)
nx.draw(G)
plt.show()

[1]: https://i.stack.imgur.com/D7qte.png

0 Answers0