Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:not': 0.03; 'else:': 0.03; 'tree': 0.05; '21,': 0.07; 'attribute': 0.07; 'element': 0.07; 'error:': 0.07; 'none:': 0.07; 'received:209.85.219': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'element,': 0.16; 'parameter.': 0.16; 'wrote:': 0.18; 'stefan': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'skip:e 30': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'xml': 0.29; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'saludos': 0.31; 'file': 0.32; 'skip:c 30': 0.32; 'up.': 0.33; 'worked': 0.33; '(most': 0.33; 'skip:& 30': 0.33; 'received:209.85': 0.35; 'received:google.com': 0.35; 'thanks': 0.36; 'subject:?': 0.36; 'received:209': 0.37; 'skip:& 10': 0.38; 'recent': 0.39; 'does': 0.39; 'how': 0.40; 'simply': 0.61; 'receive': 0.70; 'cater': 0.84; 'faith': 0.91; 'url:cn': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=VYFrPye5B+7C2fpo+bikTbr5zjPOUSxoRwZc4OVDfv0=; b=hYbFEvbpIaWhqMGfcZIU8fuRfqj1yhX9ZzNbNaSsAjqc1p3R2mi69VUdmBpvpkz6oW CzGNmLskuekxPg1tAMGwAp39SPj3Ql5frJz/0qIu2Ta2JsR7CpjFFaxe+4s0jsBPhPnh 60dj9Mzjjqm5H2EgRGetJyFxCoWm1U3D5r/HxorSpnQF15cNW8ohZS3UBdkORjTuueux svymlZNgIMQx5tgnSSu2jiA7Nkz8p5i0EVpcpYy5TyX6DXaVGxqnwP0HStmETM2vsJZC YTpdbe2Qs6Y/ry59/g8f2Y1HVh1jaEa8ijnPTvdCUFOnLE4/G2bqwmoNzVS94f/xlgHk GWWA== X-Received: by 10.182.219.136 with SMTP id po8mr8394700obc.75.1367236249025; Mon, 29 Apr 2013 04:50:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <556FF090-DABC-4A90-800F-6E3D81B6C80F@vectorfabrics.com> References: <556FF090-DABC-4A90-800F-6E3D81B6C80F@vectorfabrics.com> From: Ombongi Moraa Fe Date: Mon, 29 Apr 2013 14:50:08 +0300 Subject: Re: xml.etree.ElementTree if element does not exist? To: Stefan Holdermans Content-Type: multipart/alternative; boundary=089e0153853af2837804db7e7e8b Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367236624 news.xs4all.nl 15889 [2001:888:2000:d::a6]:54154 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44496 --089e0153853af2837804db7e7e8b Content-Type: text/plain; charset=ISO-8859-1 Hi Stefan, Group Thanks for the thumbs up. Worked perfectly. Saludos Ombongi Moraa Faith On 29 April 2013 14:22, Stefan Holdermans wrote: > Ombongi, > > > however, if i pass xml data that DOES NOT contain sepid element, i get > an error: > > > > Traceback (most recent call last): > > File "/usr/local/bin/receive.py", line 21, in > > sepid = content.find(".//{ > http://www.huawei.com.cn/schema/common/v2_1}sepid").text > > AttributeError: 'NoneType' object has no attribute 'text' > > > > > > some messages i receive will have the sepid parameter, other will not > have this parameter. How can i cater for this? kinda like an if .. else > implementation for xml.etree.ElementTree ? > > What about simply testing whether the value returned by find is None? For > example: > > $ cat test.py > from xml.etree import ElementTree > > myTree = ElementTree.fromstring('') > myElement = myTree.find('orange') > > if myElement is None: > print 'tree does not contain a child element "orange"' > else: > print myElement.text > > > $ python test.py > tree does not contain a child element "orange" > > HTH, > > Stefan --089e0153853af2837804db7e7e8b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi Stefan, Group

Thanks for the t= humbs up.

Worked perfectly.

Saludos

Ombongi Moraa Faith



On 29 April 2013 14:22, Stefan Holderman= s <stefan@vectorfabrics.com> wrote:
Ombongi,

> however, if i pass xml data that DOES NOT contain sepid element, i get= an error:
>
> Traceback (most recent call last):
> =A0 File "/usr/local/bin/receive.py", line 21, in <module= >
> =A0 =A0 sepid =3D content.find(".//{http://www.huawei.com.cn= /schema/common/v2_1}sepid").text
> AttributeError: 'NoneType' object has no attribute 'text&#= 39;
>
>
> some messages i receive will have the sepid parameter, other will not = have this parameter. How can i cater for this? kinda like an if .. else imp= lementation for xml.etree.ElementTree =A0?

What about simply testing whether the value returned by find is None?= For example:

=A0 $ cat test.py
=A0 from xml.etree import ElementTree

=A0 myTree =3D ElementTree.fromstring('<test />')
=A0 myElement =3D myTree.find('orange')

=A0 if myElement is None:
=A0 =A0 =A0 print 'tree does not contain a child element "orange&q= uot;'
=A0 else:
=A0 =A0 =A0 print myElement.text


=A0 $ python test.py
=A0 tree does not contain a child element "orange"

HTH,

=A0 Stefan

--089e0153853af2837804db7e7e8b--