JSP XML Data Processing
When sending XML data over HTTP, it becomes necessary to use JSP to handle incoming and outgoing XML documents, such as RSS documents. As an XML document is just a pile of text, creating an XML document with JSP is no harder than creating an HTML document.
Sending XML with JSP
Sending XML content with JSP is the same as sending HTML content. The only difference is that you need to set the page's context attribute to text/xml. To set the context attribute, use the<%@page % > command, like this:
<%@ page contentType="text/xml" %>
The following example sends XML content to the browser:
<%@ page contentType="text/xml" %>
Padam History
ZARA
100
Access this example with different browsers to see the document tree it presents.
Processing XML in JSP
Before processing XML with JSP, you need to place the two library files related to XML and XPath in the
- XercesImpl.jar: download it herehttp://www.apache.org/dist/xerces/j/
- xalan.jar: download it herehttp://archive.apache.org/dist/xml/xalan-j/
books.xml file:
Padam History
ZARA
100
Great Mistry
NUHA
2000
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" %>
JSTL x:parse Tags
Books Info:
The title of the first book is:
The price of the second book:
Visit http://localhost:8080/main.jsp, and the result is as follows:
BOOKS INFO:
The title of the first book is:Padam History
The price of the second book: 2000
Formatting XML with JSP
This is the XSLT stylesheet style.xsl file:
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" %>
JSTL x:transform Tags
Books Info:
Padam History
ZARA
100
Great Mistry
NUHA
2000
The running result is as follows:

For more about processing XML with JSTL, seeJSP Standard Tag Library。