0

I'm stuck, I've written a code that looks for specific index in xml file. But when find that Index won't create me a new xml file with just that Index in and constant parameters. it returns an error:

...rba_u_xml.py", line 29, in <module>
ObjectDictionary.remove(Variable)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 337, in remove
self._children.remove(element)
ValueError: list.remove(x): x not in list

this is my code:

import xml.etree.ElementTree as ET
tree = ET.parse('master.xml')
root = tree.getroot()

s = input('Insert a number of index and add quotes(") befor and after: ')
i = int(s, 16)

for MagicNumber in root.findall('MagicNumber'):   #constant parameters
    print MagicNumber.tag,':', MagicNumber.text
    print('\n')
for FileInfo in root.find('FileInfo'):
    print FileInfo.tag,':', FileInfo.text
print''
for DeviceInfo in root.find('DeviceInfo'):
    print DeviceInfo.tag,':', DeviceInfo.text
print''
for DummyUsage in root.find('DummyUsage'):
    print DummyUsage.tag,':', DummyUsage.text
print''
for ObjectDictionary in root.find('ObjectDictionary'):
    name=ObjectDictionary.get('name')
    print ObjectDictionary.tag,':', ObjectDictionary.text, name
    print''                                                       #constant parameters

    for Variable in ObjectDictionary.iter('Variable'):          #searching for Variables in index
        if Variable.find('./Index').text == str(i):
            print 'Variable name: ', Variable.attrib['name']
        elif Variable.find('./Index').text != str(i):
            ObjectDictionary.remove(Variable)

    for Record in ObjectDictionary.iter('Record'):             #searching for Records in index
        if Record.find('./Index').text == str(i):
            print 'Record name: ', Record.attrib['name']
        elif Record.find('./Index').text != str(i):
            ObjectDictionary.remove(Record)

    for Array in ObjectDictionary.iter('Array'):               #searching for Arrays in index
        if Array.find('./Index').text == str(i):
            print 'Array name: ', Array.attrib['name']
        elif Array.find('./Index').text != str(i):
            ObjectDictionary.remove(Array)


tree.write('output.xml')            #That would be amended to create a new xml file

Thanks for your help! I hope you understand(in the other case ask) because I use google translator a lot :)

5
  • Well, Variable is not in ObjectDictionary. Commented May 6, 2014 at 6:56
  • where else could be? i tried with root but still the same error... Commented May 6, 2014 at 7:00
  • @user3540670 Side note: Read PEP 008 legacy.python.org/dev/peps/pep-0008 and try following naming rules proposed there. Your naming conventions are a bit odd for Python code and make understanding it more difficult. Commented May 6, 2014 at 7:21
  • I don't think that's the main reason. have anybody any other solution? Commented May 6, 2014 at 10:57
  • I solved it(on half)! the solution I past in ansewr Commented May 8, 2014 at 12:11

1 Answer 1

0

I solved it! :D Insted of xml.etree.ElementTree modul I use xml module and insted of ObjectDictionary.remove(Variable) I use Variable.clear()

I use lxml module

from lxml import etree as ET
tree = ET.parse('master.xml')
root = tree.getroot()

-

s = input('Insert a number of index and add quotes(") befor and after: ')
i = int(s, 16)

for MagicNumber in root.findall('MagicNumber'):   #constant parameters
    print MagicNumber.tag,':', MagicNumber.text
    print('\n')
for FileInfo in root.find('FileInfo'):
    print FileInfo.tag,':', FileInfo.text
print''
for DeviceInfo in root.find('DeviceInfo'):
    print DeviceInfo.tag,':', DeviceInfo.text
print''
for DummyUsage in root.find('DummyUsage'):
    print DummyUsage.tag,':', DummyUsage.text
print''
for ObjectDictionary in root.find('ObjectDictionary'):
    name=ObjectDictionary.get('name')
    print ObjectDictionary.tag,':', ObjectDictionary.text, name
    print''                                                       #constant parameters

    for Variable in ObjectDictionary.iter('Variable'):          #searching for Variables in index
        if Variable.find('./Index').text == str(i):
            print 'Variable name: ', Variable.attrib['name']
        elif Variable.find('./Index').text != str(i):
            Variable.clear()                         #insted of ObjectDictionary.remove(Variable)

    for Record in ObjectDictionary.iter('Record'):             #searching for Records in index
        if Record.find('./Index').text == str(i):
            print 'Record name: ', Record.attrib['name']
        elif Record.find('./Index').text != str(i):
            Record.clear()                             #insted of ObjectDictionary.remove(Record)

    for Array in ObjectDictionary.iter('Array'):               #searching for Arrays in index
        if Array.find('./Index').text == str(i):
            print 'Array name: ', Array.attrib['name']
        elif Array.find('./Index').text != str(i):
            Array.clear()                               #insted of ObjectDictionary.remove(Array)


tree.write('output.xml')            #create a new xml file

In new xml file is now left just tags of variables,records and arrays.

<Group name="OptionalObjects"> <Array/><Array/><Variable/><Variable/><Variable/><Record/><Record/><Record/><Record/><Record/><Record/><Record/><Record/></Group>

Have anybody any idea how to solved out this (I hope) last problem?

Sign up to request clarification or add additional context in comments.

Comments

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.