Изменение XML-файла в python
У меня есть небольшая проблема, и я не знаю, как ее исправить. В файле 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>