By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,025 IT Pros & Developers. It's quick & easy.

Using elementtree: replacing nodes

P: n/a
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é

May 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
André wrote:
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[i] 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>

May 2 '06 #2

P: n/a
Fredrik Lundh wrote:
André wrote:
I've started using elementtree and don't understand how to use it to
manipulate and replace nodes.

[snip]
It was mostly the following step which I couldn`t figure out...
# 2) mutate the pre element
pre.clear()
pre.tag = "form"
pre[:] = [form]
[snip]
another approach is to build a parent map; see the parent_map code
at

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


Thanks!
</F>


May 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.