Populate Web forms with JSP and XML

It's possible to populate JavaServer Pages (JSPs) with XML data. Here's how.

This article originally appeared as an XML e-newsletter.

By Brian Schaffner

We have shown you how to populate both C# forms and ASP.NET forms with XML data. This tip will show you a similar technique for the Java platform. You'll learn how to populate JavaServer Pages (JSPs) with XML data.

The sample XML files

Listing A and Listing B show two sample XML documents that contain basic information. You should place these documents in a directory called C:\xmldocs, which we'll reference in our code:

Listing A: sample1.xml
  <Name>John Doe</Name>
  <Address>900 N. Michigan Ave</Address>

Listing B: sample2.xml
  <Name>Bill Gates</Name>
  <Address>1 Microsoft Way</Address>

Selecting the XML document

The first step in our sample application will be to select the XML file. We'll start by displaying a page to the user with the names of the files in the C:\xmldocs directory. The user will select one of the documents, which we'll use to populate a JSP form.

The code for SelectFile.jsp is shown in Listing C. If you're using the Tomcat server, you can place this file in your webapps/ROOT directory:

Listing C: SelectFile.jsp
<%@ page import="*" %>

File dir = new File ( "C:\\xmldocs" ) ;
String[] dirfiles = dir.list( ) ;

for (int i = 0; i < dirfiles.length; i++) {
     out.println("<a href=\"ShowFile.jsp?filename=" + dirfiles[i] + "\">" + dirfiles[i] + "</a><br>\n");

This relatively simple JSP displays a list of files. Each file is a link to the ShowFile.jsp page, which we'll use to display the contents of the XML file in an HTML form.

Displaying the XML data

In order to display the XML data in the HTML form, we'll need to parse the XML document. We'll use the filename selected in the SelectFile.jsp page to determine which document to parse. Then, we'll create a new Document object based on the underlying physical XML document. We'll use the new Document to locate specific values within the XML and show them within the HTML form. This process is shown in Listing D:

Listing D: ShowFile.jsp
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*" %>
Document doc;

String getXMLValue(String name) {
.....//Node nodeName = DOMUtils.getChild(nodeRoot, name);
.....//String value = (nodeName == null) ? null : DOMUtils.getTextValue(nodeName);
.....NodeList nlist=doc.getElementsByTagName(name);
.....String value = nlist.item(0).getFirstChild().getNodeValue();
.....return value;


String xmlfile = "C:\\xmldocs\\" + request.getParameter("filename");

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();

String name = getXMLValue("Name");
String address = getXMLValue("Address");
String city = getXMLValue("City");
String zip = getXMLValue("Zip");
String phone = getXMLValue("Phone");


<table border="0">
  <tr><td>Name:</td><td><input type="text" name="name" value="<%=name%>"></td></tr>
  <tr><td>Address:</td><td><input type="text" name="address" value="<%=address%>"></td></tr>
  <tr><td>City:</td><td><input type="text" name="city" value="<%=city%>"></td></tr>
  <tr><td>Zip:</td><td><input type="text" name="zip" value="<%=zip%>"></td></tr>
  <tr><td>Phone:</td><td><input type="text" name="phone" value="<%=phone%>"></td></tr>

Within the ShowFile.jsp page, we perform a couple of basic tasks. First, we define our imports, which include the XML parser and the document objects. We also define a simple local method for extracting a value from the XML document called getXMLValue(). (This is not an extremely robust method, but it illustrates a simple approach to parsing the XML document.)

Next, we pad our filename with the directory where our XML documents are stored. Then, we create our Document object by parsing the XML file. Notice that we established doc as a global variable in our method declaration stanza. That allows us to use doc both in the method and in the page.

Finally, we extract the specific values from the XML document and populate an HTML form with the values. If you also place the ShowFile.jsp page in your webapps/ROOT directory under Tomcat, you should be able to run the example.

Brian Schaffner is an associate director for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Technology Consulting practice.

Editor's Picks