JSP : traitement des données XML
Lors de l'envoi de données XML via HTTP, il est nécessaire d'utiliser JSP pour traiter les documents XML entrants et sortants, tels que les documents RSS. En tant que document XML, ce n'est qu'un tas de texte ; utiliser JSP pour créer un document XML n'est pas plus difficile que de créer un document HTML.
Utilisation de JSP pour envoyer du XML
L'envoi de contenu XML avec JSP est exactement comme l'envoi de contenu HTML. La seule différence est que vous devez définir l'attribut context de la page sur text/xml. Pour définir l'attribut context, utilisez la directive <%@page % >, comme ceci :
<%@ page contentType="text/xml" %>
L'exemple suivant envoie du contenu XML au navigateur :
<%@ page contentType="text/xml" %>
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
</books>
Utilisez différents navigateurs pour accéder à cet exemple et voir l'arborescence du document rendue par cet exemple.
Traitement du XML en JSP
Avant de traiter du XML avec JSP, vous devez placer les deux fichiers de bibliothèque liés à XML et XPath dans le répertoire <Tomcat Installation Directory>\lib :
- XercesImpl.jar : télécharger icihttp://www.apache.org/dist/xerces/j/
- xalan.jar : télécharger icihttp://archive.apache.org/dist/xml/xalan-j/
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>
Accédez à http://localhost:8080/main.jsp, et le résultat est le suivant :
BOOKS INFO: The title of the first book is:Padam History The price of the second book: 2000
Utiliser JSP pour formater du XML
Voici le fichier de feuille de style 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>
Voici 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>
Le résultat est le suivant :

Pour plus d'informations sur le traitement de XML avec JSTL, veuillez consulterJSP Standard Tag Library。
Autres extensions