473,657 Members | 2,756 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.parseSt ring(xml)
document <xml.dom.minido m.Document instance at 0x4181df0c> minidom.getElem entsByTagName(' teste') element = document.getEle mentsByTagName( 'teste')
element [<DOM Element: teste at 0x418e110c>] element[0].nodeType

1

Juliano Freitas
Jul 18 '05 #1
4 17805
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.parseSt ring(xml)
element = document.getEle mentsByTagName( 'teste')
textelt=element[0].firstChild
print textelt.nodeTyp e, textelt.nodeVal ue

and it will print:

3 texto

--Irmen
Jul 18 '05 #2
Juliano Freitas <ju*****@atlas. ucpel.tche.br> wrote in message news:<ma******* *************** *************** *@python.org>.. .
How can i get the text between the <teste> tags??
xml = """<root><teste > texto </teste></root>"""
from xml.dom import minidom
document = minidom.parseSt ring(xml)
document <xml.dom.minido m.Document instance at 0x4181df0c> minidom.getElem entsByTagName(' teste') element = document.getEle mentsByTagName( '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 NonvalidatingRe ader
doc = NonvalidatingRe ader.parseStrin g("<root><teste > texto </teste></root>", 'urn:dummy') print doc.xpath('stri ng(/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.parseSt ring(xml)
document<xml.dom.minid om.Document instance at 0x4181df0c> minidom.getElem entsByTagName(' teste') element = document.getEle mentsByTagName( '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.i t> 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*@doxd esk.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
3127
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 = '1.0'?> <person><who>scott</who></person>
9
33493
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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="/"> <xsl:value-of select="current()"/>
4
5915
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 would move out of the bottom screen area, I would want to move it up by that fraction so that it displays at the bottom of the screen. To do this, I would need to acquire the height of the SELECT element. I tried quite a few methods. ...
21
3963
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
6
13352
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... html file (generated using .NET 2.0 beta2): <form method="post" action="Test2.aspx" id="form1">
7
2086
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 so it uses the same element name? This is driving me insane. On the second call to var spanElm = document.getElementById("FirstNameLengthLabel"); spanElm is set to NULL. <script language=javascript>
0
1326
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 content. To make the parts unique, the top-level element of the further content
2
55316
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. something like... <input onkeyup="if (this.value.length == this.maxlength) document.forms.elements.focus()" value="whatever" maxvalue="x" type="text" /> <input value="whatever" maxvalue="x" type="text" />
8
1943
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 version="1.0" encoding="UTF-8"?> <root> <element>Content</element> <root><element>Content</element></root> <element>Content</element>
3
2188
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> <AssessmentName>Name of Assessment</AssessmentName> <AssessmentID>AssID</AssessmentID>
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.