Latest web development tutorials

JSP traitement de données XML

Lors de l'envoi des données XML via HTTP, il est nécessaire d'utiliser JSP à manipuler des documents XML entrants et sortants, tels que le document RSS. Comme un document XML, il est juste un tas de texte seulement, créer un document XML en utilisant JSP est pas difficile que de créer un document HTML.


Envoyer XML en utilisant JSP

Envoyer le contenu XML en utilisant JSP et l'envoyer en tant que contenu HTML. La seule différence est que vous devez mettre l'attribut de contexte de page est défini sur text / xml. Pour définir l'attribut de contexte, utilisez <% @ Page%> commande, comme ceci:

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

Le contenu exemple XML suivant envoyé au navigateur:

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

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

Utilisez un autre navigateur pour accéder à cet exemple, regardez les exemples présentés dans l'arborescence du document.


Traitement XML en JSP

Avant d'utiliser le processus XML JSP, vous devez XPath et les fichiers liés à XML dans les deux bibliothèques <Répertoire d'installation Tomcat> \ lib:

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

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

Visitez http: // localhost: 8080 / main.jsp, les résultats sont les suivants:

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

Utilisez le format XML JSP

Ceci est le fichier XSLT stylesheet de 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>

Ceci est le fichier 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>

Les résultats sont les suivants:

Plus sur l' utilisation de JSTL pour gérer le contenu XML, s'il vous plaît consulter le standard JSP Tag Library .