Back to: JSP Tutorials for Beginners and Professionals
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.
- What is XML?
- Why we need XML?
- XML versus HTML
- Advantages of using XML.
- How to Create XML?
- Presenting XML Documents
- Sending XML from a JSP with Example
- Processing XML in JSP with Example
- Formatting XML with JSP with Example
- Generating XML from JSP with Example
- Generating XML from JSP and JavaBeans
- Converting XML to Server-Side Objects
- Simple API for XML (SAX)
- Document Object Model (DOM)
- 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
- In HTML, the syntax and semantics of the document are defined whereas HTML is used to create a visible presentation to the user.
- HTML documents are not well-formed whereas XML documents are well-formed.
- In HTML tag names are not case-sensitive whereas tag names are case-sensitive in XML.
- HTML displays the data whereas XML stores and transports the data.
- In HTML tags are predefined whereas in XML the tags are user-defined.
Advantages of XML
- XML documents are plain text which can be edit and viewed from any simple text editor.
- XML documents have a tree structure that is very simple to understand.
- XML documents are language-independent which can be generated in one language and can be parsed by any other language.
- XML documents are platform-independent.
- 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
Select XML Wizard, then select XML File and click on Next.
Give a proper file name and click Next.
Select how you want to create your XML file and click Next.
Select the template and click Finish.
Your XML File is created successfully.
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:
Now load the books.xml document in a browser that supports XML, you will get the following output:
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:
- XercesImpl.jar
- 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
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
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.
About the Author:
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.