python - 使用 etree.ElementTree 在 XML 文件中添加所有子元素的“元素” - Python
问题描述
我有一个 XML 文件,我需要添加所有子元素的新元素。
我有一个名为 R 的元素“deviceset”,我需要添加另一个名为“R_100_5”和“?_VALUE”=100 和“?_TOL”=5 的元素。
如何“复制”并粘贴我的“R”元素并替换名称、“?_VALUE”和“?_TOL”?
PS:要从我的根目录“标记” elementos 设备集,我需要以下迭代:root[0][3][4][0].tag
示例 XML 文件输入:
<deviceset name="R" prefix="R" uservalue="yes">
<description>Resistor Fixed - ANSI</description>
<gates>
<gate name="G$1" symbol="R" x="0" y="0"/>
</gates>
<devices>
<device name="SMD_0402(M1005)" package="F_SMD_0402(M1005)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378568/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="?_Tol"/>
<attribute name="_TYPE" value="?_Type"/>
<attribute name="_VALUE" value="?_Value"/>
</technology>
</technologies>
</device>
<device name="SMD_0603(M1608)" package="F_SMD_0603(M1608)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378565/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="?_Tol"/>
<attribute name="_TYPE" value="?_Type"/>
<attribute name="_VALUE" value="?_Value"/>
</technology>
</technologies>
</device>
</devices>
<spice>
<pinmapping spiceprefix="R">
<pinmap gate="G$1" pin="1" pinorder="1"/>
<pinmap gate="G$1" pin="2" pinorder="2"/>
</pinmapping>
</spice>
</deviceset>
示例 XML 文件输出:
<deviceset name="R" prefix="R" uservalue="yes">
<description><B>Resistor Fixed - ANSI</description>
<gates>
<gate name="G$1" symbol="R" x="0" y="0"/>
</gates>
<devices>
<device name="SMD_0402(M1005)" package="F_SMD_0402(M1005)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378568/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="?_Tol"/>
<attribute name="_VALUE" value="?_Value"/>
</technology>
</technologies>
</device>
<device name="SMD_0603(M1608)" package="F_SMD_0603(M1608)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378565/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="?_Tol"/>
<attribute name="_VALUE" value="?_Value"/>
</technology>
</technologies>
</device>
</devices>
<spice>
<pinmapping spiceprefix="R">
<pinmap gate="G$1" pin="1" pinorder="1"/>
<pinmap gate="G$1" pin="2" pinorder="2"/>
</pinmapping>
</spice>
</deviceset>
<deviceset name="R_100_5" prefix="R" uservalue="yes">
<description><B>Resistor Fixed - ANSI</description>
<gates>
<gate name="G$1" symbol="R" x="0" y="0"/>
</gates>
<devices>
<device name="SMD_0402(M1005)" package="F_SMD_0402(M1005)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378568/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="5"/>
<attribute name="_VALUE" value="100"/>
</technology>
</technologies>
</device>
<device name="SMD_0603(M1608)" package="F_SMD_0603(M1608)">
<connects>
<connect gate="G$1" pad="1" pin="1"/>
<connect gate="G$1" pad="2" pin="2"/>
</connects>
<package3dinstances>
<package3dinstance package3d_urn="urn:adsk.eagle:package:16378565/2"/>
</package3dinstances>
<technologies>
<technology name="_">
<attribute name="_TOL" value="5"/>
<attribute name="_VALUE" value="100"/>
</technology>
</technologies>
</device>
</devices>
<spice>
<pinmapping spiceprefix="R">
<pinmap gate="G$1" pin="1" pinorder="1"/>
<pinmap gate="G$1" pin="2" pinorder="2"/>
</pinmapping>
</spice>
</deviceset>
解决方案
推荐阅读
- python-3.x - 如何使用 read_sql 分离数据框中的时间戳数据?
- xml - Ansible - 更新 XML 元素值
- azure - Azure WebJobs Nuget 从版本 3.0-beta 到 3.0。不再找到 BlobTriggerAttribute
- windows-server-2016 - 故障转移 HA 设置但 VM 未迁移?
- sql - 当产品类别中的最后一个产品被删除时,如何编写删除产品类别的触发器?
- pandas - 根据列上的“或”选择 Pandas 中的行
- .htaccess - htaccess 301 将旧 url 重定向到新 url
- atom-editor - Emacs ctrl-u 函数的 Atom 等价物
- java - 如何将 TestRail 与 Java+Selenium+Eclipse+TestNG 集成?
- generics - F# 通用结构构造函数和错误 FS0670