2010-07-17 3 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이 지원하는 형식의 그래프 이미지를 작성하는 명령 줄 인터페이스를 작성했습니다. 이라고하며 유용합니다.

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.