form filling XML
Fredrik Lundh
fredrik at pythonware.com
Thu Oct 13 13:30:43 EDT 2005
"George" <buffer_88 at hotmail.com> wrote:
> How can I compare the text in the element tags <li> with the elements
> tags in filling and if they match replace the text within the elements
> tags <li> with the text in the matching element tag of fillin.
> For example Since the text Monday in form matches the Element tag
> <Monday> in fillin put maandag in the element tag <li> of Monday.
here's one way to do it:
import elementtree.ElementTree as ET
# or: import cElementTree as ET
# or: import lxml.etree import ET
form="""..."""
fillin="""..."""
form_elem = ET.XML(form)
fill_elem = ET.XML(fillin)
for elem in form_elem.findall(".//li"):
text = fill_elem.findtext(elem.text)
if text:
elem.text = text
print ET.tostring(form_elem)
using your example, this prints:
<html>
<head> <title> My Sample Web Page </title> </head>
<body bgcolor="white">
<p>
What are the weekdays?
<ol>
<li>maandag</li>
<li>dinsdag</li>
<li>woensdag</li>
<li>donderdag</li>
<li>vrijdag</li>
</ol>
</p>
</body>
</html>
links:
http://effbot.org/zone/element-index.htm
http://effbot.org/zone/celementtree.htm
http://codespeak.net/lxml/
(if you're on linux, check your local package source for elementtree
packages)
</F>
More information about the Python-list
mailing list