首页 > 解决方案 > 使用 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>&lt;B&gt;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>&lt;B&gt;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>

标签: pythonxmlappendadd

解决方案


推荐阅读