Class FormToXMLTag

All Implemented Interfaces:
javax.servlet.jsp.tagext.IterationTag,, javax.servlet.jsp.tagext.Tag

public class FormToXMLTag
extends javax.servlet.jsp.tagext.TagSupport

This class is a custom tag that renders the JWPFormToXML() Javascript function which can be called to generate an XML document (technically, a string of XML, not a true XML document) from an HTML form.

This tag uses the following attributes:

renderScriptTags - true/false - When set to true, the Javascript will be rendered inside a <script> </script> tag pair. When set to false, this will not be done and it is expected that the <jstags:fullTrim/> tag appears inside a <script> </script> tag pair. If this attribute is not present, the script tags WILL be rendered.

It renders the following Javascript:

function JWPFormToXML(jwpftx_inForm, jwpftx_inRootElement) {
if (jwpftx_inForm == null) {
alert("JWPFormToXML: Form not passed in, or value was null");
return null;
if (jwpftx_inRootElement == null) {
alert("JWPFormToXML: Root element passed in was null");
return null;
outXML = "<" + jwpftx_inRootElement + ">";
for (i = 0; i < jwpftx_inForm.length; i++) {
ofe = jwpftx_inForm[i];
ofeType = ofe.type.toUpperCase();
ofeName =;
ofeValue = ofe.value;
if (ofeType == "TEXT" || ofeType == "HIDDEN" ||
ofeType == "PASSWORD" || ofeType == "SELECT-ONE" ||
ofeType == "TEXTAREA") { outXML += "<" + ofeName + ">" + ofeValue + ""
if (ofeType == "RADIO" && ofe.checked == true) {
outXML += "<" + ofeName + ">" + ofeValue + ""
if (ofeType == "CHECKBOX") {
if (ofe.checked == true) {
cbval = "true";
} else {
cbval = "false";
outXML = outXML + "<" + ofeName + ">" + cbval + ""
outXML += "";
outXML += "";
return outXML;

Usage example:

myXML = JWPFormToXML(document.forms['testform'],'Person');
This will return a string of XML with the element <Person> as the root, and all the fields from the form named "testForm" as child elements. For instance, if there is <input type="text" name="firstName" value="Frank"> on the form. the resultant XML would be Frank. The XML is one continuous string, there are no linebreaks and no tab indents anywhere. Only form elements of type TEXT, HIDDEN, PASSWORD, SELECT-ONE, TEXTAREA, RADIO and CHECKBOX will result in elements being added to the XML. For RADIO types, any selected radio's value will be sent. For Checkboxes, any which are checked are sent, those which are not checked are not sent.

Frank W. Zammetti.
See Also:
Serialized Form

Field Summary
private static org.apache.commons.logging.Log log
          Log instance.
private  java.lang.String renderScriptTags
          Whether to render the opening and closing script tags around the emitted Javascript.
Fields inherited from class javax.servlet.jsp.tagext.TagSupport
id, pageContext
Fields inherited from interface javax.servlet.jsp.tagext.IterationTag
Fields inherited from interface javax.servlet.jsp.tagext.Tag
Constructor Summary
Method Summary
 int doStartTag()
          Render the results of the tag.
 void setRenderScriptTags(java.lang.String inRenderScriptTags)
          renderScriptTags mutator.
Methods inherited from class javax.servlet.jsp.tagext.TagSupport
doAfterBody, doEndTag, findAncestorWithClass, getId, getParent, getValue, getValues, release, removeValue, setId, setPageContext, setParent, setValue
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


private static org.apache.commons.logging.Log log
Log instance.


private java.lang.String renderScriptTags
Whether to render the opening and closing script tags around the emitted Javascript.

Constructor Detail


public FormToXMLTag()
Method Detail


public void setRenderScriptTags(java.lang.String inRenderScriptTags)
renderScriptTags mutator.

inRenderScriptTags - renderScriptTags.


public int doStartTag()
               throws javax.servlet.jsp.JspException
Render the results of the tag.

Specified by:
doStartTag in interface javax.servlet.jsp.tagext.Tag
doStartTag in class javax.servlet.jsp.tagext.TagSupport
Return code.
javax.servlet.jsp.JspException - If anything goes wrong

Copyright 2005 Frank W. Zammetti