Presents your XML E-NEWSLETTER for March 26, 2003 <-------------------------------------------> POPULATE WEB FORMS WITH JSP AND XML 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 John Doe
900 N. Michigan Ave
Chicago IL 60614 630-555-5555
Listing B: sample2.xml Bill Gates
1 Microsoft Way
Redmond WA 98052 425-882-8080
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="java.io.*" %> <% File dir = new File ( "C:\\xmldocs" ) ; String[] dirfiles = dir.list( ) ; for (int i = 0; i < dirfiles.length; i++) { out.println("" + dirfiles[i] + "
\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(); doc=db.parse(xmlfile); String name = getXMLValue("Name"); String address = getXMLValue("Address"); String city = getXMLValue("City"); String zip = getXMLValue("Zip"); String phone = getXMLValue("Phone"); %>
Name:
Address:
City:
Zip:
Phone:
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. ----------------------------------------