473,506 Members | 16,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

form filling XML

How can I do the following in python:

given two strings:
form="""
<html>
<head> <title> My Sample Web Page </title> </head>
<body bgcolor="white">
<p>
What are the weekdays?
<ol>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
</ol>
</p>
</body>
</html>
"""
fillin="""
<dutchdays>
<Monday>maandag</Monday>
<Tuesday>dinsdag</Tuesday>
<Wednesday>woensdag</Wednesday>
<Thursday>donderdag</Thursday>
<Friday>vrijdag</Friday>
<Saturday>zaterdag</Saturday>
<Sunday>zondag</Sunday>
</dutchdays>
"""

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.

Kind of a Pseudo Code

import xml.dom.minidom

def xmlform(form=None, fillin=None):
Fo = xml.dom.minidom.parseString(form)
Fi = xml.dom.minidom.parseString(fillin)

Fo information:
get element tags for li
get text for li

Fi information:
get childtags for dutchdays

if(text for li=child tags for dutchdays):
replace child tags for dutchdays text with text for li

There needs to be a loop but I cannot figure out what type of loop
maybe a while len(Fo)>0: to process through the form.

Thanks for the help!!

Oct 13 '05 #1
2 1842
"George" <bu*******@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>

Oct 13 '05 #2
George wrote:

[form]
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
[fillin]
<dutchdays>
<Monday>maandag</Monday>
<Tuesday>dinsdag</Tuesday>
<Wednesday>woensdag</Wednesday>
[...]
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.


You need to first get each of the li elements in form - something which
is best done using XPath:

from xml.dom.minidom import parseString
import xml.xpath
form_doc = parseString(form)
for li in xml.xpath.Evaluate("//li", form_doc):
Do something with the element.

Here, the XPath expression "//li" means "get all li elements in the
document". When you're examining an li element, you need to get the
textual content in order to find out which day it is you want to
replace. I recommend the following:

li.normalize()
day_text = li.childNodes[0]
day_str = day_text.nodeValue

With this, you have the string value of the day (rather than some
node), and can then go looking for that day's Dutch translation in the
other document using similar techniques. Since you look for the
translation for each li element, you're going to be using nested loops
rather than a single one.

I hope this gets you started on the problem, anyway!

Paul

Oct 13 '05 #3

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

Similar topics

1
2580
by: Martin Mrazek | last post by:
Hi, I put almost ten pages long HTML form on our server. It sends filled data into DB. Filling takes almost one hour and users complain that they cannot save the partly filled form and continue...
2
1777
by: hennakapoor | last post by:
How do the form filling software determine which text field gets what data i.e. a typical form filling software will ask the user to enter the username / address / password etc. Whenever it goes to...
1
2087
by: jobi.joy | last post by:
I have a requirement of having the capability of offline form filling for my website.So users can some way download the form and can fill out in offline mode. Any standard way to do this. I am...
3
3932
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
1
9844
by: Hananiel | last post by:
I have a User control that I load dynamically into a Form. The designer has no property to let me dock it. How do i Dock it or get it to stretch out to fill. Setting this.Dock = DockStyle.Fill; in...
10
4393
by: B-Dog | last post by:
I want to run something once my form in completely loaded and drawn but running at the end of form load doesn't do I guess cause of the large dataset fill and the form isn't fully loaded. Where...
1
2532
by: Galka | last post by:
Hello I have a form to enter names and some other personal information. When a name is entered, it is checked against existing records: maybe, such name was entered before? If yes, user is...
1
4763
by: NumberCruncher | last post by:
Hi All, I am struggling with setting up my first system of tables, forms,and reports, and could use your help! I am setting up a database to keep track of the production of a produced item. The...
10
1903
by: Anil Gupte | last post by:
I am playing a Windows Media File which is protected by their DRM. I want to be able to automatically fill in the form asking for payment with my saved parameters. Is there any way to do this? ...
2
1781
by: GarthRodgers | last post by:
Hey all I'm designing an online exam booking system in Access and need to find a way to import table data into Roboform (or some other form filling package) straight from the database. The only...
0
7220
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
7105
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
7308
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
5617
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,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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...

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.