我有一点问题,我不知道如何解决它。在output.xml文件中必须只是我正在寻找的参数(变量,记录,数组),但最后是output.xml文件,就像master.xml文件一样...为什么? 我们正在寻找以十进制为单位的index =" 1018" ... index4120 请不要通过我这样的链接: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>