Python 构建XML文档结构图
我想构建一个图表,显示在给定的XML文档中,哪些标记被用作其他标记的子标记 我编写此函数是为了获取lxml.etree树中给定标记的唯一子标记集:Python 构建XML文档结构图,python,xml,graph,lxml,dotfiles,Python,Xml,Graph,Lxml,Dotfiles,我想构建一个图表,显示在给定的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()
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
是否有一个通用的图形生成器,我可以使用这个函数来构建点文件或其他格式的图形
我还暗自怀疑,在某个地方有一个专门为此目的而设计的工具;那可能是什么?我最终使用了。最后,我还使用构建了一个命令行界面,该界面从XML文档中提取一些基本信息,并以支持的格式构建图形图像。它被称为并且有点有用:
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.