943,520 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1371
  • Python RSS
May 5th, 2009
0

Iterating and removing an element from ElementTree

Expand Post »
Hi. I have an xml file like this:

xml Syntax (Toggle Plain Text)
  1. <tree>
  2. <category title="Item 1">item 1 text
  3. <subitem title="subitem1">subitem1 text</fileitem>
  4. <subitem title="subitem2">subitem2 text</fileitem>
  5. </category>
  6.  
  7. <category title="Item 2">item 2 text
  8. <subitem title="subitem21">subitem21 text</fileitem>
  9. <subitem title="subitem22">subitem22 text</fileitem>
  10. </category>
  11. </tree>

I use ElementTree to parse this file and this works okay, however, I now need to remove an item based on its title.

I can loop through the data, but for the life of me I cant work out how to remove the item once I have matched it:

python Syntax (Toggle Plain Text)
  1. import xml.etree.cElementTree as ET
  2. ...
  3. ...
  4. ...
  5. self.xml = ET.parse(self.fpath)
  6. ...
  7. ...
  8. ...
  9. titem = "subitem21"
  10. xml = self.xml.getroot()
  11. iterator = xml.getiterator("subitem")
  12. for item in iterator:
  13. f = item
  14. text = item.attrib["title"]
  15. if text == titem:
  16. xml.remove(f)

If I use print statements I can see that I am actually finding the right item, but I just cant work out how to remove it, since it would seem that I should use either xml.remove() or item.remove(), and in either case I get an error stating that "x is not in the list", presumably saying that it cant find and delete the item that it just found!

Can anyone point me in the right direction please?

cheers

Max
Reputation Points: 10
Solved Threads: 1
Light Poster
MaxVK is offline Offline
46 posts
since Nov 2008
May 5th, 2009
0

Re: Iterating and removing an element from ElementTree

According to documentation:
Quote ...
Unlike the findXYZ methods this method compares elements based on the instance identity, not on tag value or contents.
Which means that likely your iterator is returning the contents and not the instance itself.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 5th, 2009
0

Re: Iterating and removing an element from ElementTree

Hmm, okay, I can run with that - Now how do I get a reference to the instance rather than the contents?
Reputation Points: 10
Solved Threads: 1
Light Poster
MaxVK is offline Offline
46 posts
since Nov 2008
May 5th, 2009
0

Re: Iterating and removing an element from ElementTree

Hmm, after taking another look at your method, you must be getting the instance and not just the contents since you're able to call the attrib method... Have you tried using remove(item) instead of remove(f) ?
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 5th, 2009
0

Re: Iterating and removing an element from ElementTree

Yes. I get the same error, which is simply that the item is not in the list. Iv tried getting the element half a dozen ways, and so far that error is the closest that Iv come to removing an item.

If I just try to "print item" it shows that it is an element, but whichever way I treat it, I get the same error. Kind of driving me nuts now!

[edit]
More specifically I get "ValueError: list.remove(x): x not in list" as the error.

regards

Max
Last edited by MaxVK; May 5th, 2009 at 6:03 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
MaxVK is offline Offline
46 posts
since Nov 2008
May 7th, 2009
0

Re: Iterating and removing an element from ElementTree

Okay, the problem has been solved with some help from others. The problem was that the item I wanted to delete was a child item, and I needed to use the parent to perform the remove:

parent.remove(child)

I ended up with some much simple code, that works nicely providing you keep in mind where in the structure the child to be removed actually is - This code works to remove direct Grandchildren of the Root item! (Of course its easy enough to expand it to remove any items, but Ill leave that to you!)

Python Syntax (Toggle Plain Text)
  1. for x in xml:
  2. for y in x:
  3. if y.attrib["title"] == titem:
  4. x.remove(y)
  5. self.xml.write("output.xml")
  6. return

I hope this helps someone.

regards

Max
Reputation Points: 10
Solved Threads: 1
Light Poster
MaxVK is offline Offline
46 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: "Blue Screen"
Next Thread in Python Forum Timeline: 2 programs involving lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC