JSP XML-Datenverarbeitung
Wenn über HTTP XML-Daten zu senden, ist es erforderlich, JSP zu verwenden, eingehende und ausgehende XML-Dokumente zu verarbeiten, wie RSS-Dokument. Als XML-Dokument ist es nur ein Haufen von nur Text, ein XML-Dokument erstellen mit JSP nicht schwierig ist, ein HTML-Dokument als zu schaffen.
XML Senden JSP
Senden von XML-Inhalten JSP mit und senden Sie es als HTML-Inhalte. Der einzige Unterschied ist, dass Sie die Seite Kontextattribut setzen müssen, um Text / xml gesetzt. Um das Kontext Attribut gesetzt, <% @ page%> Befehl wie folgt verwenden:
<%@ page contentType="text/xml" %>
Das nächste Beispiel XML-Inhalte an den Browser gesendet:
<%@ page contentType="text/xml" %> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> </books>
Verwenden Sie einen anderen Browser dieses Beispiel zuzugreifen, Blick auf die Beispiele in den Dokumentenbaum dargestellt.
Verarbeitung von XML in JSP
Vor der Verwendung von XML die JSP-Prozess, müssen Sie XPath und XML-Dateien in den beiden Bibliotheken <Tomcat Installationsverzeichnis> \ lib:
- XercesImpl.jar: In diesem Download http://www.apache.org/dist/xerces/j/
- xalan.jar: In diesem Download http://xml.apache.org/xalan-j/index.html
books.xml Datei:
<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 Datei:
<%@ 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>
Besuchen Sie http: // localhost: 8080 / main.jsp, Ergebnisse sind wie folgt:
BOOKS INFO: The title of the first book is:Padam History The price of the second book: 2000
Verwenden Sie JSP XML-Format
Dies ist die XSLT-Stylesheet style.xsl Datei:
<?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>
Dies ist main.jsp Datei:
<%@ 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>
Ergebnisse sind wie folgt:
Mehr zur Verwendung von JSTL XML - Inhalte zu handhaben , wenden Sie sich bitte JSP Standard - Tag - Bibliothek .