473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get element text in DOM?

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
Jul 18 '05 #1
4 17763
Juliano Freitas wrote:
How can i get the text between the <teste> tags??

xml = """<root><teste> texto </teste></root>"""


You must know that the text between the tags is a DOM element
by itself, namely a TEXT node, which is a child of the
elment node formed by the tag.

So try;

xml = """<root><teste> texto </teste></root>"""
from xml.dom import minidom
document = minidom.parseString(xml)
element = document.getElementsByTagName('teste')
textelt=element[0].firstChild
print textelt.nodeType, textelt.nodeValue

and it will print:

3 texto

--Irmen
Jul 18 '05 #2
Juliano Freitas <ju*****@atlas.ucpel.tche.br> wrote in message news:<ma**************************************@pyt hon.org>...
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


http://lists.fourthought.com/piperma...er/013027.html

Verbatim:

"""
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/developerwork...ematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xm...y/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/xm...x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerwork...rary/x-stand4/
Jul 18 '05 #3
On Wed, 10 Nov 2004 17:11:09 -0200, Juliano Freitas
<ju*****@atlas.ucpel.tche.br> 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].nodeType1

Here is an useful function I have written:

def getText(node, recursive = False):
"""
Get all the text associated with this node.
With recursive == True, all text from child nodes is retrieved
"""
L = ['']
for n in node.childNodes:
if n.nodeType in (dom.Node.TEXT_NODE,
dom.Node.CDATA_SECTION_NODE):
L.append(n.data)
else:
if not recursive:
return None
L.append( get_text(n) )

return ''.join(L)
print getText(element[0])



Regards Manlio Perillo
Jul 18 '05 #4
Manlio Perillo <NO******************@libero.it> wrote:
for n in node.childNodes:
if n.nodeType in (dom.Node.TEXT_NODE, dom.Node.CDATA_SECTION_NODE):
(Aside: node.TEXT_NODE would probably be better here. Can't guarantee
that a DOM's implementation of the 'Node' interface is available as a
class called 'Node' inside its module.)
L.append(n.data)
else:
if not recursive:
return None


Surely 'continue'? This will exit the function (returning None instead
of the expected empty string) the first time a non-Text node is met.

Incidentally, DOM Level 3 Core defines the property 'textContent' to
return pretty much exactly this (although it removes the ignorable
whitespace). Not in minidom yet, but... <insert usual plug here>

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3114
by: Martin | last post by:
Hallo, can you help me writing a generic xslt transformation (useable with xsql from oracle)? The problem is how to get the escaping characters .... === INPUT-File in.xml <?xml version =...
9
33476
by: Stefan Franke | last post by:
Hi, I've got the following simple XSLT stylesheet, that lists all the values of the elements of any given XML file. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0"...
4
5903
by: Rithish | last post by:
Is there a way to obtain the height of a <SELECT> element dynamically, i.e. through javascript? I want to dynamically display a list box onFocus of a text box element. Also, if the list box...
21
3930
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
6
13334
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
7
2069
by: David | last post by:
Hi, I'd really appreciate any help. Im trying to change a label over and over again. I can change it the first time, by using the following code, however it never works again, how can i do this...
0
1316
by: ruediger | last post by:
Hi there, I want to set up an XML Schema for documents of the following structure: The <root> node contains - an optional <message> element with a text attribute, and - arbitrary further...
2
55221
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
8
1926
by: VK | last post by:
Can be multiple instances of element used as the root element? That's a curly way of asking, but I did not come up with a better sentence, sorry. What I mean is with a document like: <?xml...
3
2181
markmcgookin
by: markmcgookin | last post by:
Hi, I have the following XML <AnswerList xmlns="http://tempuri.org/ALPS_Assessmentv1p1_RESCO_Schema.xsd"> <DateTimeLastSaved>12:12:12 1900</DateTimeLastSaved> <UserName>Bob</UserName>...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.