Given the previous XML document, you can use a JSP to read the XML document and retrieve data from it for display. The JSP performs these tasks:
Obtains the XML document and converts it into a DOM document.
Finds every
order
node with account number 86666.Displays an HTML table with a row for each
order
node that satisfies the selection criteria as follows:Order ID
Order Status
ID101010
Shipped
ID101011
Shipped
Transforming XML to DOM
The JSP processes the XML document by calling the servlet bean XMLToDOM. XMLToDom obtains the XML document widget
orders
and converts it into a DOM document. The servlet bean’s input
parameter is set to orders.xml
—the relative URL of the XML document:
<%-- Convert the XML document into a DOM object --%> <dsp:droplet name="/atg/dynamo/droplet/xml/XMLToDOM"> <dsp:param name="input" value="orders.xml"/> <dsp:param name="validate" value="false"/> <%--if conversion succeeds--%> <dsp:oparam name="output"> ... </dsp:oparam> <%--if conversion fails--%> <dsp:oparam name="failure"> ... </dsp:oparam> </dsp:droplet>
XMLToDom has two open parameters whose bodies conditionally execute, depending on whether the transformation succeeds or fails:
output
specifies how to process the DOM document when transformation succeeds.failure
specifies the message to display if the transformation fails.
Processing DOM nodes
If XMLToDOM succeeds, it executes the contents of its open parameter output
. In this example, the output
body calls the servlet bean NodeForEach, which iterates over the specified DOM nodes and performs these tasks:
Gets each
order
node whoseaccount
attribute value is 86666.Gets the node’s
id
attribute and sets its value in the appropriate HTML table cell.
<%--if conversion succeeds--%> <dsp:oparam name="output"> <%--from first order node, select each node where account = 86666--%> <dsp:droplet name="/atg/dynamo/droplet/xml/NodeForEach"> <dsp:param name="node" param="document.firstChild"/> <dsp:param name="select" value="/widget-orders[position()=1]/order[@account='86666']"/> <%-- Case where none found --%> <dsp:oparam name="empty"> No orders found. </dsp:oparam> <%-- if data found, start output --%> <dsp:oparam name="outputStart"> <table> <tr> <td>Order ID</td> <td>Order Status</td> </tr> </dsp:oparam> <%-- output value of order node's id attribute --%> <dsp:oparam name="output"> <tr> <td> <dsp:valueof param="element.id.nodeValue"/> </td> <%-- get child node order-status, display its value --%> <dsp:droplet name="/atg/dynamo/droplet/xml/NodeMatch"> <dsp:param name="node" param="element"/> <dsp:param name="match" value="order-status/text()"/> <%-- Case where match is successful --%> <dsp:oparam name="output"> <td> <dsp:valueof param="matched.nodeValue"/> </td> </dsp:oparam> </dsp:droplet> </tr> <%-- end NodeForEach output --%> </dsp:oparam> <%-- close table --%> <dsp:oparam name="outputEnd"> </table> </dsp:oparam> <%-- close NodeForEach droplet --%> </dsp:droplet> <%-- close XMLToDOM output --%> </dsp:oparam>
Handling DOM conversion failure
If the XML-to-DOM conversion fails, the XMLToDOM servlet bean executes its open parameter failure
:
<%-- If conversion fails --%> <dsp:oparam name="failure"> Failure to parse XML document: <dsp:valueof param="input"/> <br/> </dsp:oparam>