在 python 中修改 XML 文件

2024-03-13

我有一个小问题,我不知道如何解决它。在output.xml文件中必须只是我正在寻找的参数(变量,记录,数组),但最终output.xml文件与master.xml文件相同......为什么? 我们正在寻找十进制的index=“1018”...index4120 请不要给我发这样的链接:https://docs.python.org/2/library/xml.etree.elementtree.html https://docs.python.org/2/library/xml.etree.elementtree.html因为我经历了3000次

import xml.etree.ElementTree as ET

tree = ET.parse('master.xml')
root = tree.getroot()

variables = root.iter('Variable')
records = root.iter('Record')
arrays = root.iter('Array')
s = input('insert a hex number of index and add (") befor and after number: ')
i = int(s, 16)


for node in variables:
    if node.find('./Index').text == str(i):
        print 'name of variable: ', node.attrib['name']
    elif node.find('./Index').text != str(i):
        root.remove(variables)
for node in records:
    if node.find('./Index').text == str(i):
        print 'name of record: ', node.attrib['name']
    elif node.find('./Index').text != str(i):
        root.remove(records)
for node in arrays:
    if node.find('./Index').text == str(i):
        print 'name of array: ', node.attrib['name']
    elif node.find('./Index').text != str(i):
        root.remove(arrays)

tree.write('output.xml')
<ObjectDictionary>
    <Group>
        <Variable name="Device Type">
            <Index>4096</Index>
            <Subindex>0</Subindex>
            <DataType>7</DataType>
            <AccessType>3</AccessType>
            <ObjectType>7</ObjectType>
            <DefaultValue>0x0000000</DefaultValue>
            <PDOMapping>0</PDOMapping>
        </Variable>
        <Variable name="Error Register">
            <Index>4097</Index>
            <Subindex>0</Subindex>
            <DataType>5</DataType>
            <AccessType>0</AccessType>
            <ObjectType>7</ObjectType>
            <PDOMapping>0</PDOMapping>
        </Variable>
        <Record name="Identity Object">
            <Index>4120</Index>
            <ObjectType>9</ObjectType>
            <Variable name="number of entries">
                <Index>4120</Index>
                <Subindex>0</Subindex>
                <DataType>5</DataType>
                <AccessType>0</AccessType>
                <ObjectType>7</ObjectType>
                <DefaultValue>4</DefaultValue>
                <PDOMapping>0</PDOMapping>
            </Variable>
            <Variable name="Vendor Id">
                <Index>4120</Index>
                <Subindex>1</Subindex>
                <DataType>7</DataType>
                <AccessType>0</AccessType>
                <ObjectType>7</ObjectType>
                <DefaultValue>0x0</DefaultValue>
                <PDOMapping>0</PDOMapping>
            </Variable>
            <Variable name="Product Code">
                <Index>4120</Index>
                <Subindex>2</Subindex>
                <DataType>7</DataType>
                <AccessType>0</AccessType>
                <ObjectType>7</ObjectType>
                <DefaultValue>0x0</DefaultValue>
                <PDOMapping>0</PDOMapping>
            </Variable>
            <Variable name="Revision number">
                <Index>4120</Index>
                <Subindex>3</Subindex>
                <DataType>7</DataType>
                <AccessType>0</AccessType>
                <ObjectType>7</ObjectType>
                <PDOMapping>0</PDOMapping>
            </Variable>
            <Variable name="Serial number">
                <Index>4120</Index>
                <Subindex>4</Subindex>
                <DataType>7</DataType>
                <AccessType>0</AccessType>
                <ObjectType>7</ObjectType>
                <PDOMapping>0</PDOMapping>
            </Variable>
        </Record>
    </Group>
</ObjectDictionary>

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 python 中修改 XML 文件 的相关文章

随机推荐