Using elementtree: replacing nodes

  • Thread starter =?iso-8859-1?B?QW5kcuk=?=
  • Start date
?

=?iso-8859-1?B?QW5kcuk=?=

I've started using elementtree and don't understand how to use it to
manipulate and replace nodes. I know how to do this using a simple,
but inefficient parser I wrote, but I'd rather learn to use a better
tool - especially as it is to be added to the standard library.

I'll use a simple, but representative example. Suppose I have the
following text, where I inserted blank lines to isolate the part I want
to manipulate.
===================
<html>
<body>
<p>
This is some text.
</p>

<pre class="text">
a = 6*7
print a
</pre>

<p>
Done.
</p>
</body>
</html>
====================
Now, I would like to find all <pre> tags of the "text" class and
1. change the class value
2. append another node ( <textarea> )
3. surround the both nodes by a third one ( <form> )
==================
<html>
<body>
<p>
This is some text.
<p>

<form>
<pre class="text2">
a = 6*7
print a
</pre>
<textarea cols=80 name="code">
</textarea>
<input type="submit"></input>
</form>

<p>
Done.
</p>
</body>
</html>
===============

Any help would be appreciated.
André
 
F

Fredrik Lundh

André said:
I've started using elementtree and don't understand how to use it to
manipulate and replace nodes. I know how to do this using a simple,
but inefficient parser I wrote, but I'd rather learn to use a better
tool - especially as it is to be added to the standard library.
Now, I would like to find all <pre> tags of the "text" class and

for pre in elem.getiterator("pre"):

or

for pre in elem.findall(".//pre"):
1. change the class value

pre.set("class", "value")
2. append another node ( <textarea> )
3. surround the both nodes by a third one ( <form> )

or in other words,

2. replace the <pre> with a <form> element that contains some
new contents derived from the old element

# 1) build the new form
form = Element("form")
e = SubElement(form, "pre")
e.set("class", "text2")
e.text = pre.text
e = SubElement(form, "textarea", cols=80, name="code")
e = SubElement(form, "input", type="submit")

# 2) mutate the pre element
pre.clear()
pre.tag = "form"
pre[:] = [form]

for harder cases, a common pattern is:

for parent in elem.getiterator():
for index, child in enumerate(parent.findall("pre")):
# the code in here can manipulate parent and child

another approach is to build a parent map; see the parent_map code
at

http://effbot.org/zone/element.htm#the-element-type

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,202
Latest member
ClaudioVil

Latest Threads

Top