The following document is a simple name and address entry with embedded XML tags.Notice the hierarchical embedding of tags, each with an ending tag. Notice, also, the first, xml line in which <? ?> brackets are used. Finally, notice that name_ is used as a tag; hard-won knowledge is that name is a reserved keyword and, even though context should indicate this, cannot be used as a label.
<?xml version="1.0"?> <entry> <name_> <name_first>John </name_first> <name_middle>B. </name_middle> <name_last>Smith </name_last> </name_> <address_> <address_1>100 Evans Ct.</address_1> <city>Carrboro</city> <state>NC</state> <zip>27510</zip> </address_> </entry>
Basic XML Concepts
The discussion below discusses issues for creating an xml document that is intended to be mapped to an html form.
XML Document
An xml document is defined by an explicit xml comment tag:
<?xml version='1.0'?>It must be the first line in an xml file.
Bounding Tags
A key xml concept is that the tags identify logical classes of information; they do not describe the specific appearance or representation of those data.
A good practice is to "wrap" your document in a pair of tags that identify the type of information and, possibly, the overall type of document. For example, you may wish to indicate that the data are to be represented as a form that allows user input versus a report that does not.
The following tags suggest that the data is a "data object" intended to be represented as a form.
<form_data_object> ... </form_data_object>General Information
Frequently, you will wish to include general information for the object, such as a title or, perhaps, a system identifier. these data may or may not be included (by the xsl stylesheet) in the html generated (and displayed to the user).
Below are several examples of such tags:
<data_object_name>Person</data_object_name> <data_object_id>1017757816904</data_object_id> <response_message>Search succeeded; number of entries found: 4</response_message>Repeated Information
Frequently, the data will included repeated information. That is, a form may included several different input fields, a table several rows, etc. A good practice is to bound a set of repeated items with a pair of tags and then to mark each repeated item identically.
Below are several examples of such tags:
<entries> <item> <field> <label>PersonID</label> <name>personID</name> <value>1015944886291</value> </field> <field> <label>First Name</label> <name>nameFirst</name> <value>john</value> </field> <field> <label>Middle Name</label> <name>nameMiddle</name> <value>b</value> </field> </item> </entries>In this example, the design would allow for multiple items to be included within entries, each of which includes multiple fields. Each field, in turn, is composed of the same set of elements -- label, name, and value -- repeated for all of the fields.
Generating the XML
Generating the XML in the model component of the architecture is simply a matter of constructing the appropriate java String object. Doing so is illustrated in the following example method.
public String formFilled ( ViewBean viewBean ) { String xmlString = "<?xml version='1.0'?>\n"; xmlString += "<form_data_object>\n"; xmlString += "<data_object_name>" + viewBean.getTargetName() + "</data_object_name>\n"; xmlString += "<data_object_id>" + viewBean.getObjectID() + "</data_object_id>\n"; xmlString += "<response_message>" + viewBean.getMessage() + "</response_message>\n"; xmlString += "<entries>\n"; xmlString += "<item>\n"; xmlString += viewBean.getXML(); xmlString += "</item>\n"; xmlString += "</entries>\n"; xmlString += "</form_data_object>\n"; return xmlString; }