2010-07-17 22 views
4

Je voudrais construire un graphique montrant quelles balises sont utilisées comme enfants des autres balises dans un document XML donné.Construction d'un graphique de la structure d'un document XML

J'ai écrit cette fonction pour obtenir l'ensemble unique de balises enfants pour un tag donné dans un arbre 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 

Y at-il un constructeur graphique à usage général que je pourrais utiliser cette fonction pour construire un fichier de point ou un graphique dans un autre format? Je vois également la suspicion qu'il existe un outil conçu explicitement à cette fin; Qu'est-ce que ça pourrait être?

Répondre

0

J'ai fini par utiliser python-graph. J'ai également fini par utiliser argparse pour créer une interface de ligne de commande qui extrait des informations de base de documents XML et construit des images graphiques dans des formats pris en charge par pydot. Il est appelé xmlearn et est en quelque sorte 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.