473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

p & br using ElementTree?

Hello all,

This is probably a mind numbingly brain dead question.. But how do I
generate the following:

<p>Current date:<br/>2000-01-01</p>

..using ElementTree? The <pelement kind of needs two text blocks,
as far as I can tell?

--
Kind regards,
Jan Danielsson
Jun 27 '07 #1
3 2021
Jan Danielsson wrote:
Hello all,

This is probably a mind numbingly brain dead question.. But how do I
generate the following:

<p>Current date:<br/>2000-01-01</p>

..using ElementTree? The <pelement kind of needs two text blocks,
as far as I can tell?
Use the .tail attribute on the br element:

In [1]: from xml.etree import ElementTree as ET

In [4]: p = ET.Element('p')

In [5]: p.text = 'Current date:'

In [6]: br = ET.SubElement(p, 'br')

In [7]: br.tail = '2000-01-01'

In [8]: ET.dump(p)
<p>Current date:<br />2000-01-01</p>

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '07 #2
Robert Kern wrote:
[---]
><p>Current date:<br/>2000-01-01</p>
[---]
Use the .tail attribute on the br element:

In [1]: from xml.etree import ElementTree as ET

In [4]: p = ET.Element('p')

In [5]: p.text = 'Current date:'

In [6]: br = ET.SubElement(p, 'br')

In [7]: br.tail = '2000-01-01'

In [8]: ET.dump(p)
<p>Current date:<br />2000-01-01</p>
That did the trick. Thanks!

--
Kind regards,
Jan Danielsson
Jun 27 '07 #3
En Wed, 27 Jun 2007 01:12:10 -0300, Jan Danielsson
<ja**************@gmail.comescribió:
This is probably a mind numbingly brain dead question.. But how do I
generate the following:

<p>Current date:<br/>2000-01-01</p>

..using ElementTree? The <pelement kind of needs two text blocks,
as far as I can tell?
No, the date string goes into br's tail:

pyimport xml.etree.ElementTree as ET
pyp=ET.fromstring
pyp=ET.fromstring("<p>Current date:<br/>2000-01-01</p>")
pyp
<Element p at b6a850>
pyp.text
'Current date:'
pyp.tail
pyp[0]
<Element br at b6aa58>
pyp[0].text
pyp[0].tail
'2000-01-01'

See <http://effbot.org/zone/element-infoset.htmabout infosets and the
"mixed content" simplified model.

--
Gabriel Genellina
Jun 27 '07 #4

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

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
0
by: Mark E. Smith | last post by:
cElementTree cannot hold ElementTree instances. #from elementtree.ElementTree import ElementTree, dump # This works from cElementTree import ElementTree, dump # This does not from...
0
by: Mark | last post by:
-------- Original Message -------- Subject: Using cElementTree and elementtree.ElementInclude Date: Mon, 23 Oct 2006 09:40:24 -0500 From: Mark E. Smith <mark.e.smith@arnold.af.mil> Organization:...
2
by: Rick Muller | last post by:
I'm a computational chemist who frequently dabbles in Python. A collaborator sent me a huge XML file that at one point was evidently modified by a now defunct java application. A sample of this...
1
by: Mike Slinn | last post by:
The following short Python program parses a KML file and displays the names of all Marks and Routes: from elementtree.ElementTree import ElementTree tree = ElementTree(file='test.kml') kml =...
4
by: Ouray Viney | last post by:
Xml <ib>8.4.27.5</ib> python from xml.dom import minidom xmldoc = minidom.parse('C:\TestProfile.xml') xmldoc
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.