Passing values in ComboBox from XML file

This page discusses - Passing values in ComboBox from XML file

Passing values in ComboBox from XML file

Passing values in ComboBox from XML file

In this tutorial we are going to know how we can pass a values in ComboBox by using XML

This example will examine how to parse and expose XML information using the JAXP with a JSP page. This tutorial is only geared towards showing how to construct a Java object from an XML document. For this what we need  a XML file in which we have some data which needs to be retrieved. What we need to learn is to retrieve the data from the XML file and insert it into the ComboBox.

To make a program over it we have used the following page directive attributes, the first attribute we have used is contentType and the second attribute is import. The content type tells about the MIME type and character encoding the JSP file uses for the response it sends to the client. We need to import some classes and packages which we have described in the tutorial. 

Inside the scriptlet firstly we will define a factory API which allows our application to obtain a Java XML  parser. DocumentBuilderFactory defines a factory API. Now create a DocumentBuilder object to parse an org.w3c.dom.Document from XML. Save the xml file at bin file of  C:\apache-tomat-5.5.23\bin\. Now we need to parse method to actually parse the XML file to create our own Document object. Document object will be created by parse method of the DocumentBuilder

From the Document object we will get a NodeList object represents all of the elements in our XML document name message. NodeList object will be obtained by the getElementByTagName(). 

A NodeList is an array starting from 0 and going up to the length of the array by calling getFirstChild() method. By using this method the desired text node is returned and we can use getNodeValue() for our message. As we are inserting the values in the combobox so we are using the <select> tag to represent the combobox

The code of the passingcomboboxXML.xml file:

<?xml version="1.0" encoding="UTF-8"?>
 <values>
 <value1>1</value1>
 <value2>2</value2>
 <value3>3</value3>
 <value4>4</value4>
 <value5>5</value5>
 <value6>6</value6>
 <value7>7</value7>
 <value8>8</value8>
 <value9>9</value9>  
  </values>

Code of the passingValuseInCombox.jsp file.

<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"
%>
<html>
<body>
     <center><H1>Passing values in COMBOBOX</h1></center>
<center><table border="2">
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("\\passingcomboboxXML.XML");
NodeList value1= doc.getElementsByTagName("value1");
NodeList value2= doc.getElementsByTagName("value2");
NodeList value3= doc.getElementsByTagName("value3");
NodeList value4= doc.getElementsByTagName("value4");
NodeList value5= doc.getElementsByTagName("value5");
NodeList value6= doc.getElementsByTagName("value6");
NodeList value7= doc.getElementsByTagName("value7");
NodeList value8= doc.getElementsByTagName("value8");
NodeList value9= doc.getElementsByTagName("value9");
%> 
<form method="POST" >
     <tr><td>
  <select size="1" name="D1">
    <option><%= value1.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value2.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value3.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value4.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value5.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value6.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value7.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value8.item(0).getFirstChild().
getNodeValue() %></option>
	<option><%= value9.item(0).getFirstChild().
getNodeValue() %></option>
	</select>
   </td></tr><tr><td>
   <input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
   <% out.println(request.getParameter("D1")); %>
   </td></tr>
</form> 
</table>
 </center>
</body>
</html>

The output of the program is given below:

 

 

Download passingValuseInCombox.jsp file.

Download passingcomboboxXML.xml.txt