Costruire un grafico della struttura di un documento XML
Domanda
Mi piacerebbe costruire un grafico che mostra quali tag sono usati come bambini di cui altri tag in un dato documento XML.
Ho scritto questa funzione per ottenere il set unico dei tag per bambini per un determinato tag in un albero LXML.etree:
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
.
Esiste un generatore di grafico per uso generico che potrei usare con questa funzione per costruire un DotFile o un grafico in qualche altro formato?
Sto anche ottenendo il sospetto di furia che c'è uno strumento da qualche parte progettato esplicitamente per questo scopo;Cosa potrebbe essere?
Soluzione
Ho finito per usare Python-grafico .Ho finito anche usando argparrse per creare un'interfaccia della riga di comando che tira alcuni bit di base diINFORMAZIONI DA XML Documenti e creazione di immagini del grafico in formati supportati da Pydot .Si chiama xmlearn ed è utile:
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.
.