JSP Procesamiento de datos XML
Cuando se envían datos XML a través de HTTP, es necesario utilizar JSP para procesar los documentos XML entrantes y salientes, como los documentos RSS. Como documento XML, no es más que un montón de texto; crear un documento XML con JSP no es más difícil que crear un documento HTML.
Enviar XML con JSP
Enviar contenido XML con JSP es igual que enviar contenido HTML. La única diferencia es que debe establecer el atributo context de la página en text/xml. Para establecer el atributo context, utilice el comando <%@page % >, de la siguiente manera:
<%@ page contentType="text/xml" %>
A continuación, este ejemplo envía contenido XML al navegador:
<%@ page contentType="text/xml" %>
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
</books>
Acceda a este ejemplo con diferentes navegadores y observe el árbol de documentos que presenta.
Procesar XML en JSP
Antes de procesar XML con JSP, debe colocar los dos archivos de biblioteca relacionados con XML y XPath en el directorio <Tomcat Installation Directory>\lib:
- XercesImpl.jar: descargar aquíhttp://www.apache.org/dist/xerces/j/
- xalan.jar: descargar aquíhttp://archive.apache.org/dist/xml/xalan-j/
Archivo books.xml:
<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>
Archivo main.jsp:
<%@ 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>
Acceda a http://localhost:8080/main.jsp, el resultado es el siguiente:
BOOKS INFO: The title of the first book is:Padam History The price of the second book: 2000
Formatear XML con JSP
Este es el archivo de hoja de estilo XSLT 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>
Este es el archivo main.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:8080/style.xsl" var="xslt"/>
<x:transform xml="${xmltext}" xslt="${xslt}"/>
</body>
</html>
El resultado de la ejecución es el siguiente:

Para obtener más información sobre el procesamiento de XML con JSTL, consulteBiblioteca de etiquetas estándar de JSP。
Otras extensiones