Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP XML data processing


May 12, 2021 JSP


Table of contents


JSP XML data processing

When sending XML data over HTTP, it is necessary to use JSP to process incoming and outflowd XML documents, such as RSS documents. As an XML document, it's just a bunch of text, and creating an XML document with JSP is no more difficult than creating an HTML document.


Send XML using JSP

Sending XML content using JSP is the same as sending HTML content. T he only difference is that you need to set the context property of the page to text/xml. To set the context property, use the command of @page% and %, as this is:

<%@ page contentType="text/xml" %>

The next example sends XML content to the browser:

<%@ page contentType="text/xml" %>

<books>
   <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
   </book>
</books>

Use a different browser to access this example and see the document tree presented in this example.


Handle XML in JSP

Before you can work with XML with JSP, you need to place two library files related to XML and XPath in the directory of the Tomcat Design Directory:

Books .xml files:

<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>

Main .jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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:8080/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>

Access the http://localhost:8080/main.jsp and the results are as follows:

BOOKS INFO:
The title of the first book is:Padam History 
The price of the second book: 2000

Format XML using JSP

This is the XSLT style sheet style sheet style.xsl file:

<?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>

This is the main .jsp file:

<%@ 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:8080/style.xsl" var="xslt"/>
<x:transform xml="${xmltext}" xslt="${xslt}"/>
 
</body>
</html>

The results are as follows:

JSP XML data processing

For more information about using JSTL to work with XML, check out the JSP Standard Label Library.