[Tutor] getting text within an element [attribute text] using elementtree

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jan 20 01:48:53 CET 2006



On Fri, 20 Jan 2006, ps python wrote:

> In my XML file some attributes have text within an element. I want to
> get that text. How can I do that.


Hello,

The problems you have don't have to do with the calls to gettext, but to
selecting namespaced nodes as with your previous questions.  Let's look at
some of the code:

> >>> for m in
> tree.findall('//{org:hprd:dtd:hprdr2}proteinInteractor'):
> ...     k = gettext(m)
> ...     print k
> ...

The XML that you show us appears to use a different namespace than
'org:hprd:dtd:hprdr2':

######################################
<entrySet xmlns="net:sf:psidev:mi" ...
######################################

We should be using "net:sf:psidev:mi" rather than "org:hprd:dtd:hprdr2"
when we look for those XML elements.  The first refers to the Proteomics
Standards Initiative, which is different than the one whose data you've
been working with earlier.  So be a bit more aware of the role of
namespaces.


Anyway, your question was about getting at attributes.  Have you looked at
the get() method of elements?  There's documentation at:

    http://effbot.org/zone/element.htm#attributes


Here's a small example:

######
>>> text = """<foo xmlns="some-prefix">
...           <message>Hello world</message>
...           <message name="attr1">This has attributes</message>
...           </foo>"""
>>> from elementtree import ElementTree
>>> tree = ElementTree.fromstring(text)
>>> tree
<Element {some-prefix}foo at 81c976c>
>>>
>>> for msg in tree:
...     if msg.get('name'):
...         print msg.get('name')
...         print msg.text
...
attr1
This has attributes
######


Good luck!



More information about the Tutor mailing list