Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31566

Remove parent node -namespace issue

Date 2012-10-18 14:20 +1100
Subject Remove parent node -namespace issue
From Tharanga Abeyseela <tharanga.abeyseela@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2383.1350530426.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Remove parent node -namespace issue Tharanga Abeyseela <tharanga.abeyseela@gmail.com> - 2012-10-18 14:20 +1100

csiph-web