Latest web development tutorials

JSP elaborazione di dati XML

Quando si inviano dati XML attraverso HTTP, è necessario utilizzare JSP per gestire documenti XML entrata e in uscita, come documento RSS. Come un documento XML, è solo un mucchio di solo testo, creare un documento XML usando JSP non è difficile che creare un documento HTML.


Invia XML utilizzando JSP

Invia il contenuto XML utilizzando JSP e inviarlo come contenuto HTML. L'unica differenza è che è necessario mettere l'attributo contesto di pagina è impostata su text / xml. Per impostare l'attributo di contesto, usare <@ pagina%%> comando, in questo modo:

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

Il prossimo contenuto esempio XML inviato al browser:

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

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

Utilizzare un browser per accedere a questo esempio, guardate gli esempi presentati nel documento.


XML Processing in JSP

Prima di utilizzare il processo di XML JSP, è necessario XPath e file XML correlati nelle due librerie <Tomcat directory di installazione> directory \ lib:

file 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>

File 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>

Visita http: // localhost: 8080 / main.jsp, i risultati sono i seguenti:

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

Utilizzare il formato XML JSP

Questo è il file di fogli di stile 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>

Questo è il file 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>

I risultati sono i seguenti:

Ulteriori informazioni sull'uso di JSTL per gestire il contenuto XML, si prega di consultare Biblioteca JSP Standard Tag .