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


Groups > comp.lang.python > #64653

Elementree and insert new element if it is not present

Date 2014-01-24 12:44 +1100
Subject Elementree and insert new element if it is not present
From Tharanga Abeyseela <tharanga.abeyseela@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5925.1390527865.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,

I have the  following xml,a nd i need to add Description element if it is
not present.

<TVSeries><Provider>xxx</Provider><Title>X</Title><SortTitle>World's
Fastest Indian, The</SortTitle></TvSeries>
<Movies><Provider>xxx</Provider><Title>The World's Fastest
Indian</Title><Description> The World's Fastest Indian
</Description><SortTitle>World's Fastest Indian,
The</SortTitle></Movies>

here is my function.

def insert_description(root,type):
        for child in root.findall('.//{
http://schemas.microsoft.com/xxx/2011/06/13/ingest}%s' % type):
                title=child.find('.//{
http://schemas.microsoft.com/xxx/2011/06/13/ingestion}Title').text
                try:
                        if child.find('Description') is None:
                                new_desc = ET.Element('Description')
                                new_desc.text = title
                                 child.insert(1, new_desc)

                except:
                        pass

root is the xm lfile, type includes (movies,shows,dramas etc)

But this will add the description without checking the "Description" is
exist. i have a problem with the following statement. (i guess)

if child.find('Description') is None:

what i'm doing wrong here, appreciate your help

Regards,
Tharanga

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


Thread

Elementree and insert new element if it is not present Tharanga Abeyseela <tharanga.abeyseela@gmail.com> - 2014-01-24 12:44 +1100

csiph-web