Java Reference
In-Depth Information
}
public static void main(String args[]){
File f = new File("Detail.xml");
int id = 1000;
DetailPageXMLBean xmlBean = new DetailPageXMLBean();
xmlBean.setId(id);
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(xmlBean.getVehicleData());
}catch(Exception e){
e.printStackTrace();
}
}
}
Although this is a simple approach to generating XML, it saves the overhead of building a DOM object
and serializing it. The getXmlString() method is simply a convenience for use in the simple JSP
page shown in Listing 15-8 . Similarly, the main method is included to let you check out the XML by
dumping it to a file.
Cross-
Reference
The advantages of the DOM-based approach to handling XML are
discussed in Part IV , which deals with XML and Java databases.
The resulting XML can be displayed in a browser using the simple JSP page shown in Listing 15-8 .
Notice the use of the contentType attribute in the <% @ page %> directive. This attribute is required
so that the browser can recognize the data as XML and display it accordingly.
Listing 15-8: JSP page using a JavaBean to display a ResultSet as XML
<%@ page language="java" contentType="text/xml"%>
<jsp:useBean id="DetailPageXMLBean"
class="JavaDatabaseBible.ch15.DetailPageXMLBean" scope="session"/>
<jsp:setProperty name="DetailPageXMLBean" property="*"/>
<%=DetailPageXMLBean.getXmlString()%>
The resulting XML is shown in Listing 15-9 . Although the structure shown here is very simple, with no
nested elements, everything discussed in the examples applies equally to more complicated XML
documents.
Listing 15-9: ResultSet formatted as XML
<?xml version="1.0"?>
<VehicleData>
<ID>1000</ID>
<Make>Honda</Make>
<Body>Coupe</Body>
<Model>Civic</Model>
<Year>1996</Year>
<Color>Red</Color>
Search WWH ::




Custom Search