RE:ElementTree Issue - Search and remove elements

Discussion in 'Python' started by Tharanga Abeyseela, Oct 18, 2012.

  1. Hi Guys,

    Managed to find the element from my xml feed. but i'm having an issue
    when trying to remove the parent node and write the xml file.

    #!/usr/bin/env python

    import elementtree.ElementTree as ET

    tree = ET.parse('test.xml')
    root = tree.getroot()
    walkAll = tree.getiterator()

    for elt in walkAll:

    for x in
    elt.findall('.//{http://schemas.xxxx.com/xxx/2xx/0x/xx/ixxxx}Rating'):

    if x.text=="NC":
    tree.remove(elt)
    else:
    tree.write('out.xml')

    i was trying to remove the element (not the element tag).

    why i cant remove the elt element from root if i found the NC matching pattern.

    thanks,
    tharanga
     
    Tharanga Abeyseela, Oct 18, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kee Nethery
    Replies:
    12
    Views:
    2,093
    Stefan Behnel
    Jun 27, 2009
  2. Tharanga Abeyseela

    ElementTree Issue - Search and remove elements

    Tharanga Abeyseela, Oct 17, 2012, in forum: Python
    Replies:
    2
    Views:
    156
    Stefan Behnel
    Oct 17, 2012
  3. Stefan Behnel
    Replies:
    0
    Views:
    166
    Stefan Behnel
    Oct 17, 2012
  4. Tharanga Abeyseela
    Replies:
    0
    Views:
    139
    Tharanga Abeyseela
    Oct 17, 2012
  5. Tharanga Abeyseela

    re:ElementTree Issue - Search and remove elements

    Tharanga Abeyseela, Oct 17, 2012, in forum: Python
    Replies:
    0
    Views:
    149
    Tharanga Abeyseela
    Oct 17, 2012
Loading...

Share This Page