0

i have a little problem and i don't know how to fix it. In the output.xml file must be just parametrs(Variables,Records,Arrays) that i'm looking for, but in the end is output.xml file the same like master.xml file...why? we are looking for index= "1018"...index4120 in decimal please don't past me a link like this : https://docs.python.org/2/library/xml.etree.elementtree.html becouse i go trough 3000x times

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>

3
  • you probably want to use raw_input instead of input to get rid of the ". Commented Apr 22, 2014 at 8:12
  • the elifs are the exact opposite of the ifs, so you can use else instead Commented Apr 22, 2014 at 8:14
  • I change everything you said but still not working Commented Apr 22, 2014 at 8:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.