[4suite] Re: get element text in DOM?

Uche Ogbuji uche.ogbuji at fourthought.com
Fri Nov 12 16:00:57 EST 2004


On Wed, 2004-11-10 at 14:20 -0500, Jeremy Jones wrote:
> Sorry - my send finger got a little trigger happy.  Let's try again.
> 
> Jeremy Jones wrote: 
> > Juliano Freitas wrote: 
> > > How can i get the text between the <teste> tags??
> > > 
> > >   
> > > > > > xml = """<root><teste> texto </teste></root>"""
> > > > > > from xml.dom import minidom
> > > > > > document = minidom.parseString(xml)
> > > > > > document
> > > > > >         
> > > <xml.dom.minidom.Document instance at 0x4181df0c>
> > >   
> > > > > > minidom.getElementsByTagName('teste')
> > > > > >         
> > > 
> > >   
> > > > > > element = document.getElementsByTagName('teste')
> > > > > > element
> > > > > >         
> > > [<DOM Element: teste at 0x418e110c>]
> > >   
> > > > > > element[0].nodeType
> > > > > >         
> > > 1
> > > 
> > > Juliano Freitas
> > > 
> > > 
> > >   
> > How about something like this:
> > 
> > 
> > 
> In [24]: from xml.dom import minidom
> 
> In [25]: x = """<root><teste> texto </teste></root>"""
> 
> In [26]: document = minidom.parseString(x)
> 
> In [27]: print document.documentElement.getElementsByTagName
> ('teste')[0].childNodes[0].data

Or, ObTopic, for 4Suite recent CVS:

>>> from Ft.Xml.Domlette import NonvalidatingReader
>>> doc = NonvalidatingReader.parseString("<root><teste> texto
</teste></root>", 'urn:dummy')
>>> print doc.xpath('string(/root/teste)')
 texto

Simple and sweet IMHO.

-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
A hands-on introduction to ISO Schematron - http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
Schematron abstract patterns - http://www.ibm.com/developerworks/xml/library/x-stron.html
Wrestling HTML (using Python) - http://www.xml.com/pub/a/2004/09/08/pyxml.html
XML's growing pains - http://www.adtmag.com/article.asp?id=10196
XMLOpen and more XML Hacks - http://www.ibm.com/developerworks/xml/library/x-think27.html
A survey of XML standards - http://www-106.ibm.com/developerworks/xml/library/x-stand4/




More information about the Python-list mailing list