加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 构建XML文档结构的图形

发布时间:2020-12-20 13:20:35 所属栏目:Python 来源:网络整理
导读:我想构建一个图表,显示哪些标签用作给定 XML文档中其他标签的子项. 我编写了这个函数来为lxml.etree树中的给定标记获取唯一的子标记集: def iter_unique_child_tags(root,tag): """Iterates through unique child tags for all instances of tag. Iteration
我想构建一个图表,显示哪些标签用作给定 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

是否有一个通用图形构建器,我可以使用此函数来构建其他格式的点文件或图形?

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

解决方法

我最终使用 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,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读