How to work with XML in JSP

How to work with XML in JSP Application

In this article, I am going to discuss How to work with XML in JSP Application. Please read our previous article where we discussed How to interact with a Database in JSP Application. At the end of this article, you will understand the following pointers.

  1. What is XML?
  2. Why we need XML?
  3. XML versus HTML
  4. Advantages of using XML.
  5. How to Create XML?
  6. Presenting XML Documents
  7. Sending XML from a JSP with Example
  8. Processing XML in JSP with Example
  9. Formatting XML with JSP with Example
  10. Generating XML from JSP with Example
  11. Generating XML from JSP and JavaBeans
  12. Converting XML to Server-Side Objects
  13. Simple API for XML (SAX)
  14. Document Object Model (DOM)
  15. Transforming XML
Overview of XML:

XML stands for Extensible Markup Language. It is derived from Standard Generalized Markup Language (SGML). It is a text-based markup language. It was released in the late 90s and was created to provide easy-to-use and store self-describing data. It is not a replacement for HTML but is designed to be self-descriptive. It is a platform-independent language. It is also a language-independent language. XML is used to carry the data but not to display the data.

What is XML?

XML is a markup language like HTML which is used to store and transport the data. XML is self-descriptive which contains sender information, receiver information, heading, and a message body. The XML does not DO anything as it is just information wrapped in tags. XML has no predefined tags whereas the user has to create their own tags. XML simplifies things by sharing data and transporting the data. It became a W3C recommendation as early as February 1998.

Why we need XML?

XML is self-describing where the structure of data is embedded with the data; so, there is no need to pre-build the structure to store the data; it is understood by the XML. XML document is “well-formed” and properly marked up. It defines the attributes and characteristics of the elements in the beginning tag of an element. An AML application is endless. XML is a simple language that can take information and consolidate it into a document called XML document, which provides proper structure to the information.

XML versus HTML
  1. In HTML, the syntax and semantics of the document are defined whereas HTML is used to create a visible presentation to the user.
  2. HTML documents are not well-formed whereas XML documents are well-formed.
  3. In HTML tag names are not case-sensitive whereas tag names are case-sensitive in XML.
  4. HTML displays the data whereas XML stores and transports the data.
  5. In HTML tags are predefined whereas in XML the tags are user-defined.
Advantages of XML
  1. XML documents are plain text which can be edit and viewed from any simple text editor.
  2. XML documents have a tree structure that is very simple to understand.
  3. XML documents are language-independent which can be generated in one language and can be parsed by any other language.
  4. XML documents are platform-independent.
  5. XML is Unicode-based which takes less space and can be transmitted efficiently.
How to Create XML in JSP Application?

Step1: Right-click on the project. Go to New -> Other

How to Create XML in JSP Application?

Select XML Wizard, then select XML File and click on Next.

Why we need XML?

Give a proper file name and click Next.

Advantages of using XML

Select how you want to create your XML file and click Next.

How to Create XML?

Select the template and click Finish.

Presenting XML Documents

Your XML File is created successfully.

How to work with XML in JSP Application

Presenting XML Documents

Let’s write an XML document that is presenting a book. Here we have one root element and every element has a start tag and a close tag. The first line in the example states the XML version number which is 1. It lets the processor know that the encoding scheme to be used here is “UTF-8”.

Boooks.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <name>Padam History</name>
  <author>ZARA</author>
  <price>100</price>
 </book>
 <book>
  <name>Great Mistry</name>
  <author>NUHA</author>
  <price>2000</price>
 </book>
</books>

The Tree Structure of the above XML file is:

Presenting XML Documents Tree Structure

Now load the books.xml document in a browser that supports XML, you will get the following output:

Working with XML in JSP Application

Sending XML from a JSP / Generating XML from JSP

We can send or generate XML documents using JSP like HTML. It will easily generate a response containing the books.xml document. Here, we need to send the content type of our page to text/XML. Use <%@page%> tag to set the content type.

Syntax: <%@ page contentType = “text/xml” %>

Example
<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <name>Padam History</name>
  <author>ZARA</author>
  <price>100</price>
 </book>
</books>
Processing XML in JSP Application

To process XML using JSP, copy the following two XML and XPath libraries into your “Tomcat Installation Directory/lib” folder:

  1. XercesImpl.jar
  2. xalan.jar
Example

In this example, books.xml is the XML file that we are processing using books.jsp file.

books.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <name>Padam History</name>
  <author>ZARA</author>
  <price>100</price>
 </book>

 <book>
  <name>Great Mistry</name>
  <author>NUHA</author>
  <price>2000</price>
 </book>
</books>
books.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

<html>
<head>
<title>JSTL x:parse Tags</title>
</head>

<body>
 <h3>Books Info:</h3>
 <c:import var="bookInfo" url="http://localhost:8086/HelloWorldJSP/books.xml" />
 <x:parse xml="${bookInfo}" var="output" />
 <b>The title of the first book is</b>:
 <x:out select="$output/books/book[1]/name" />
 <br>

 <b>The price of the second book</b>:
 <x:out select="$output/books/book[2]/price" />
</body>
</html>
Output

Processing XML in JSP Application

Formatting XML with JSP Application

Formation of an XML file with JSP file can be done using a transformation language called Extensible Stylesheet Language Transformation (XSLT) which is a part of XSL. It allows you to write XML vocabulary for specifying formatting semantics.

Example

In this example, style.xsl is an XSL stylesheet that performs the required transformation for the books element which generates HTML markup and extracts data from the elements in the books.xml document.

style.xsl
<?xml version = "1.0"?>
<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="html" indent="yes" />
 <xsl:template match="/">
  <html>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="books">
  <table border="1" width="100%">
   <xsl:for-each select="book">
    <tr>
     <td>
      <i>
       <xsl:value-of select="name" />
      </i>
     </td>

     <td>
      <xsl:value-of select="author" />
     </td>

     <td>
      <xsl:value-of select="price" />
     </td>
    </tr>
   </xsl:for-each>
  </table>
 </xsl:template>
</xsl:stylesheet>
books.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

<html>
<head>
<title>JSTL x:transform Tags</title>
</head>

<body>
 <h3>Books Info:</h3>
 <c:set var="xmltext">
  <books> <book> <name>Padam History</name> <author>ZARA</author>
  <price>100</price> </book> <book> <name>Great Mistry</name> <author>NUHA</author>
  <price>2000</price> </book> </books>
 </c:set>

 <c:import url="http://localhost:8086/JSPDemo/style.xsl" var="xslt" />
 <x:transform xml="${xmltext}" xslt="${xslt}" />
</body>
</html>
Output

working with XML in JSP Application

In the next article, I am going to discuss Generating XML from JSP and JavaBeans with Examples. Here, in this article, I try to explain How to work with XML in JSP Application and I hope you enjoy this working with XML in JSP Application.

Leave a Reply

Your email address will not be published. Required fields are marked *