Plotting the structure of an XML document

I would like to build a graph showing which tags are used as children, of which the other tags in this XML document.

I wrote this function to get a unique set of child tags for a given tag in the lxml.etree tree:

def iter_unique_child_tags(root, tag): """Iterates through unique child tags for all instances of tag. Iteration starts at `root`. """ found_child_tags = set() instances = root.iterdescendants(tag) from itertools import chain child_nodes = chain.from_iterable(i.getchildren() for i in instances) child_tags = (n.tag for n in child_nodes) for t in child_tags: if t not in found_child_tags: found_child_tags.add(t) yield t 

Is there a universal graph builder that I could use with this function to build a dotfile or a graph in a different format?

I also get a suspicious suspicion that there is a tool specifically designed for this purpose; what could it be?

+4
source share
1 answer

I ended up using python-graph . I also ended up using argparse to create a command line interface that extracts some basic bits of information from XML documents and builds graphics in formats supported by pydot . It is called xmlearn and is useful:

 usage: xmlearn [-h] [-i INFILE] [-p PATH] {graph,dump,tags} ... optional arguments: -h, --help show this help message and exit -i INFILE, --infile INFILE The XML file to learn about. Defaults to stdin. -p PATH, --path PATH An XPath to be applied to various actions. Defaults to the root node. subcommands: {graph,dump,tags} dump Dump xml data according to a set of rules. tags Show information about tags. graph Build a graph from the XML tags relationships. 
0
source

Source: https://habr.com/ru/post/1316042/


All Articles