Remove parent node -namespace issue

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

  1. Hi All,

    I have the following xml file and trying to search the word NC and
    remove the grand-parent node from the xml file and write the new
    result to a new xml.


    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <Feed xmlns="http://schemas.xxxx.xx/xx/2011/06/13/xx">
    <TVEpisode>
    <Provider>0x5</Provider>
    <ItemId>http://fxxxxxxl</ItemId>
    <Title>WWE</Title>
    <SortTitle>WWE </SortTitle>
    <Description>WWE</Description>
    <IsUserGenerated>false</IsUserGenerated>
    <Images>
    <Image>
    <ImagePurpose>BoxArt</ImagePurpose>
    <Url>https://xxxxxx.xx/@006548-thumb.jpg</Url>
    </Image>
    </Images>
    <LastModifiedDate>2012-10-16T00:00:19.814+11:00</LastModifiedDate>
    <Genres>
    <Genre>xxxxx</Genre>
    </Genres>
    <ParentalControl>
    <System>xxxx</System>
    <Rating>M</Rating>

    if i find the following match , i need to delete the <TVEpisode> or
    the other relevant node (ex <Movies>,<TVShows>) from the xml file ,
    finally need to write the new xml file.

    <Rating>NC</Rating>

    here is my code, i can find all NC words, but i cant delete the
    relevant parent node. i was using root.remove(elt) but it didnt work.
    is this a problem with namespace ?


    #!/usr/bin/env python
    import elementtree.ElementTree as ET

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

    for elt in walkAll:

    if elt.tag[0] == '{':
    elt.tag = elt.tag.split('}', 1)[1]
    for x in
    elt.findall('.//{http://schemas.XXX.com/XXXX/2011/06/13/ingestion}Rating'):
    print x.text
    if x.text=="NC":
    print "Found"
    root.remove(elt) ###### deleting
    the root node

    else:
    tree.write('out.xml') ## write the new xml with
    correct data


    Can someone please explain the parent node removing method and writing
    to the new xml file method.
    ~
    thanks in advance
    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. wolf
    Replies:
    0
    Views:
    561
  2. Replies:
    0
    Views:
    1,496
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,443
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    2
    Views:
    830
    Martin Honnen
    Mar 17, 2008
  5. John Bankhead

    Null parent node on custom tree node after populate on demand

    John Bankhead, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    286
    John Bankhead
    Dec 4, 2006
Loading...

Share This Page