【发布时间】:2021-07-01 22:31:42
【问题描述】:
我尝试操作一个不压缩的 XML 导出绘图的 diagrams.net(以前的 draw.io)。
电缆可以连接到元素上,我想要一个电缆列表。
我通过测试元素是否具有source 和target 属性来搜索所有电缆。然后我将两者的id's 与完整的元素列表进行比较,以在value 中找到连接的标签。
在有人尝试添加“插件标签”之前,这非常有效。之后(即使它已被删除),该元素将被包裹在具有id 属性的<object> 中,但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 适用于普通mxCell 格式化以查找id、source 和target 元素:
list_of_mxCell_elements = root.findall(root_node,".//*[@source][@target]")
这对于objects 元素 id 的:
list_of_objects_elements = root.findall(root_node,".//*[@source][@target]/..")
但是我怎样才能从list_of_objects_elements 访问mxCell 元素,这样我才能得到source 和target 的id?
【问题讨论】:
标签: python-3.x findall xml.etree draw.io