2010-07-17 33 views
4

我想要构建一个图表,显示哪些标记被用作给定XML文档中其他标记的子元素。构建一个XML文档的结构图

我写这个函数来获得一套独特的子标签的对于给定的标签在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 

有,我可以用这个使用通用的图形生成器函数来以某种其他格式构建点文件或图形?

我也越来越怀疑有一个工具明确地为此目的而设计;那可能是什么?

回答

0

我结束了使用python-graph。我还最终使用argparse构建了一个命令行界面,该界面从XML文档中提取一些基本信息,并以pydot支持的格式构建图形图像。它被称为xmlearn,是有用的:

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.