Python 3.x 如何在多个级别上跟踪findall()
我尝试操作diagrams.net(以前是draw.io)而不是压缩的XML导出图形 电缆可以连接到元素,我想得到一个电缆列表。 我通过测试元素是否具有Python 3.x 如何在多个级别上跟踪findall(),python-3.x,findall,xml.etree,draw.io,Python 3.x,Findall,Xml.etree,Draw.io,我尝试操作diagrams.net(以前是draw.io)而不是压缩的XML导出图形 电缆可以连接到元素,我想得到一个电缆列表。 我通过测试元素是否具有source和target属性来搜索所有电缆。然后我将两者的id与元素的完整列表进行比较,以在value中找到连接的标签 在有人尝试添加“附加标签”之前,这种方法非常有效。在此之后(即使已删除),元素将被包装在一个中,该属性具有id属性,但source和target属性保留在一个子节点中,称为: list_of_objects_elements
source
和target
属性来搜索所有电缆。然后我将两者的id
与元素的完整列表进行比较,以在value
中找到连接的标签
在有人尝试添加“附加标签”之前,这种方法非常有效。在此之后(即使已删除),元素将被包装在一个
中,该属性具有id
属性,但source
和target
属性保留在一个子节点中,称为:
list_of_objects_elements = root.findall(root_node,'.//*[@source][@target]/..')
for cable in list_of_objects_elements:
for mxCell in cable.findall('./*[@source][@target]'):
之前:
<mxCell id="ferXMembXyNwfAPwV5vA-22" value="" style="..endless list" edge="1" parent="1" source="ferXMembXyNwfAPwV5vA-8" target="ferXMembXyNwfAPwV5vA-18">
<mxGeometry relative="1" as="geometry">
<mxPoint x="540" y="520" as="sourcePoint" />
<mxPoint x="700" y="520" as="targetPoint" />
</mxGeometry>
</mxCell>
之后:
<object label="" id="ferXMembXyNwfAPwV5vA-53">
<mxCell style="..endless long list" edge="1" parent="1" source="ferXMembXyNwfAPwV5vA-42" target="ferXMembXyNwfAPwV5vA-51">
<mxGeometry relative="1" as="geometry">
<mxPoint x="660" y="340" as="sourcePoint" />
<mxPoint x="770" y="360" as="targetPoint" />
</mxGeometry>
</mxCell>
</object>
此findall
适用于格式化为查找id
、source
和target
元素的正常mxCell
:
list\u of_mxCell\u elements=root.findall(根节点“./*[@source][@target]”
对于对象
元素id:
list\u of_objects\u elements=root.findall(根节点“./*[@source][@target]/..”)
但是如何从
对象列表中访问mxCell
元素,以便获得源代码和目标代码的id?我自己找到了一个解决方案
在findall'elements'之后,我迭代元素列表,只需对我得到的每个cable元素执行另一个findall
看起来有点像这样:
list_of_objects_elements = root.findall(root_node,'.//*[@source][@target]/..')
for cable in list_of_objects_elements:
for mxCell in cable.findall('./*[@source][@target]'):
请注意略有不同的findall路径:
这将在
中搜索源和目标,同时从root返回下一个更高的元素
//*[@source][@target]/..
/*[@source][@target]
而较低的搜索范围仅在比
更深的一个元素中搜索源和目标
对我来说,这些道路仍然是一种精神打击