Latest web development tutorials

el procesamiento de datos XML JSP

Al enviar datos XML a través de HTTP, es necesario utilizar JSP para manejar documentos XML entrantes y salientes, como documento RSS. Como un documento XML, es sólo un montón de sólo texto, crear un documento XML utilizando JSP no es difícil que crear un documento HTML.


Enviar XML utilizando JSP

Enviar contenido XML utilizando JSP y enviarlo como contenido HTML. La única diferencia es que usted necesita para poner el atributo de contexto de página se establece en text / xml. Para establecer el atributo de contexto, utilizar <% @ page%> comando, así:

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

El siguiente ejemplo XML contenido enviado al navegador:

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

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

Utilice un navegador diferente para acceder a este ejemplo, mirar a los ejemplos presentados en la estructura del documento.


Procesamiento de XML en JSP

Antes de utilizar el proceso de XML JSP, necesita XPath y archivos relacionados con XML en las dos bibliotecas <Tomcat directorio de instalación> \ lib:

books.xml archivo:

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

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

Visita http: // localhost: 8080 / main.jsp, los resultados son los siguientes:

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

Utilizar el formato XML 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>

Los resultados son como sigue:

Más sobre el uso de JSTL para manejar el contenido XML, consulte JSP Standard Tag Library .