Iterating and removing an element from ElementTree

Thread Solved

Join Date: Nov 2008
Posts: 41
Reputation: MaxVK is an unknown quantity at this point 
Solved Threads: 1
MaxVK MaxVK is offline Offline
Light Poster

Iterating and removing an element from ElementTree

 
0
  #1
May 5th, 2009
Hi. I have an xml file like this:

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Iterating and removing an element from ElementTree

 
0
  #2
May 5th, 2009
According to documentation:
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 41
Reputation: MaxVK is an unknown quantity at this point 
Solved Threads: 1
MaxVK MaxVK is offline Offline
Light Poster

Re: Iterating and removing an element from ElementTree

 
0
  #3
May 5th, 2009
Hmm, okay, I can run with that - Now how do I get a reference to the instance rather than the contents?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Iterating and removing an element from ElementTree

 
0
  #4
May 5th, 2009
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) ?
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 41
Reputation: MaxVK is an unknown quantity at this point 
Solved Threads: 1
MaxVK MaxVK is offline Offline
Light Poster

Re: Iterating and removing an element from ElementTree

 
0
  #5
May 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 41
Reputation: MaxVK is an unknown quantity at this point 
Solved Threads: 1
MaxVK MaxVK is offline Offline
Light Poster

Re: Iterating and removing an element from ElementTree

 
0
  #6
May 7th, 2009
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!)

  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC