ElementTree - Howto access text within XML tag element...

cmalmqui cmalmqui at gmail.com
Tue Aug 11 22:21:20 CEST 2009


On Aug 11, 9:13 am, Ned Deily <n... at acm.org> wrote:
> In article
> <1ad8dac1-8fff-493a-a197-d847e7b6a... at c2g2000yqi.googlegroups.com>,
>
>  cmalmqui <cmalm... at gmail.com> wrote:
> > I am writing on a small XML parser and are currently stuck as I am not
> > able to get the whole element name in ElementTree.
>
> > Please see the below example where "print root[0][0]" returns
> > "<Element 'Activity' at 018A3938>"
>
> > Is there a way to get hold of the "Running" string in the tag using
> > elementTree?
>
> > <Activities>
> >     <Activity Sport="Running">
> >       <Id>2009-07-10T14:48:00Z</Id>
> >       <Lap StartTime="2009-07-10T14:48:00Z">
> >       .........
>
> "Running" is the value of the "Sport" attribute of the "Activity"
> element.  The documentation for the Element interface lists several ways
> to access element attributes; in your example,
>
> >>> elem = root[0][0]
> >>> elem.get("Sport")
> 'Running'
> >>> elem.attrib
>
> {'Sport': 'Running'}>>> elem.items()
>
> [('Sport', 'Running')]
>
> Seehttp://docs.python.org/library/xml.etree.elementtree.html
>
> --
>  Ned Deily,
>  n... at acm.org

Excellent!
Thanks!
The XML magic is getting there slowly...



More information about the Python-list mailing list