English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When sending XML data via HTTP, it is necessary to use JSP to process incoming and outgoing XML documents, such as RSS documents. As an XML document, it is just a pile of text. Creating an XML document using JSP is not more difficult than creating an HTML document.
Sending XML content using JSP is similar to 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" %>
Next, this instance sends XML content to the browser:
<%@ page contentType="text/xml" %> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> </books>
Access this instance using different browsers to see the document tree presented by this instance.
Before using JSP to process XML, you need to place two library files related to XML and XPath under the <Tomcat Installation Directory>\lib directory:
XercesImpl.jar: Download herehttp://www.apache.org/dist/xerces/j/
xalan.jar: Download herehttp://archive.apache.org/dist/xml/xalan-j/
books.xml file:
<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>/title> </head> <body> <h2>Books Info:</h2> <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>/b>: <x:out select="$output/books/book[1]/name /> <br> <b>The price of the second book</b>/b>: <x:out select="$output/books/book[2]/price /> </body> </html>
Access http://localhost:8080/main.jsp, the running result is as follows:
BOOKS INFO: The title of the first book is: Padam History The price of the second book: 2000
This is the XSLT stylesheet file 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>
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> <h2>Books Info:</h2> <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 running result is as follows:
For more information on using JSTL to process XML, please refer toJSP Standard Tag Library.