Thierry
Seunevel |
Tech Corner |
Home > Tech Corner > HTML2XML
XML generation from the content of HTML forms fields (5)
|
|
| The Javascript Code of the computeXML Function |
The computeForms.js file contains the computeXML function described in the previous pages. The Javascript of this function is shown and explained below.
|
The Javascript code |
|
function leftFill(num) {
return "" + ((num < 10) ? "0" : "") + num;
}
function bif(sFunct) {
if (sFunct == "%date") {
today = new Date();
return leftFill(today.getDate()) + leftFill(today.getMonth()+1) +
leftFill(today.getYear()).substring(2);
}
if (sFunct == "%time") {
today = new Date();
return leftFill(today.getHours()) + leftFill(today.getMinutes());
}
}
function computeXML(theForm) {
var elm="";
var formX = document.forms[0];
var nbe=0;
for (i=0; i<formX.elements.length; i++) {
if (formX.elements[i].name=="xmlversion")
var txt = "<" + formX.XMLVersion.value + ">\n";
else if (formX.elements[i].name=="element")
nbe = nbe + 1;
}
elm = new Array(nbe-1);
var j=0;
for (i=0; i<formX.elements.length; i++) {
if (formX.elements[i].name=="element") {
elm[j++]=formX.elements[i].value;
txt = txt + "<" + formX.elements[i].value + ">\n";
}
}
for (i=0 ; i< document.forms.length ; i++) {
var formX = document.forms[i];
var formN = formX.name;
if (formN.substring(0,1) != "_") {
j = formN.indexOf("__");
if (j > 0) formN = formN.substring(0,j);
var attr = "";
var elmts = "";
for (j=0 ; j<formX.elements.length ; j++) {
var elmName = formX.elements[j].name;
if (elmName.substring(0,1) != "_") {
var val = formX.elements[j].value.toUpperCase();
if (val.substring(0,1) == "%") val=bif(val);
if (val.substring(0,1) == "?") {
toDo = formX.elements[j].value.substring(1);
val = eval(toDo);}
if (val != "") {
if (elmName.substring(0,1) == "@")
attr += " " + elmName.substring(1) + "=\"" + val + "\"";
else elmts += "<" + elmName + ">" + val + "</" + elmName +">\n";
}
}
}
if (attr != "" || elmts != "") {
txt += "<" + formN + attr;
if (elmts != "") txt += ">\n" + elmts + "</" + formN + ">\n";
else txt += "/>\n";
}
}
}
for (j=nbe-1 ; j>=0 ; j--) txt += "</" + elm[j] + ">\n";
document._FormFin.xml.value = txt;
return true;
}
|
|
|
- The leftFill function is used to insert a zero before the day, month and year if the value is less than 10 (9 is returned as 09).
- The function bif (BuiltInFunction) returns a value according to the function name (no other parameter can be used).
It is invoked when a field has a value beginning with %. For our samples, the Built In Functions date and time returning respectively the current date and the current time have been defined.
- The computeXML function receives a reference to the submitted form, and fills the xml field of that form.
It looks first in the first form of the document for a field named XMLVersion and extracts its value, then counts the number of fields named ELEMENT, and create an array containing the corresponding values. The opening outmost tags are generated.
- The main loop treats each form in the document discarding those which name begins with an underscore. The name of each form is used as the XML element name (ignoring characters beyond 2 underscore).
- For each selected form, the function selects the input fields discarding those which name begins by an underscore.
For each selected field, the name and the value are extracted.
If the value begins with a %, the bif function is called with the value as parameter.
If the value begins with a ? the Javascript eval function is used to evaluate the expression.
If the field name begins with a @ the name and value are added as attribute (NAME="VALUE") to the current element, otherwise name and value are added as elements (<NAME>VALUE</NAME>.
- If elements and or attributes have been found for the current form, the starting tag is added to the XML string, then the attributes if ever, then the sub-elements, then the ending tag (</TAGNAME> or />).
- At the end of the function, the ending tags of the outmost elements stored in an array at the beginning of the function, are added to the XML string, and the xml field is given the value of the computed string.
|
|
© Thierry Seunevel (2004) |
www.seusoft.com |
|
|
|
|