{"id":223437,"date":"2021-08-06T09:18:58","date_gmt":"2021-08-06T01:18:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=223437"},"modified":"2021-07-26T10:19:36","modified_gmt":"2021-07-26T02:19:36","slug":"jsp-xml-processing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/jsp-xml-processing.html","title":{"rendered":"JSP\u7b14\u8bb0-XML \u6570\u636e\u5904\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSP\uff08\u5168\u79f0JavaServer Pages\uff09\u662f\u7531Sun Microsystems\u516c\u53f8\u4e3b\u5bfc\u521b\u5efa\u7684\u4e00\u79cd\u52a8\u6001\u7f51\u9875\u6280\u672f\u6807\u51c6\u3002JSP\u90e8\u7f72\u4e8e\u7f51\u7edc\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u54cd\u5e94\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u8bf7\u6c42\uff0c\u5e76\u6839\u636e\u8bf7\u6c42\u5185\u5bb9\u52a8\u6001\u5730\u751f\u6210HTML\u3001XML\u6216\u5176\u4ed6\u683c\u5f0f\u6587\u6863\u7684Web\u7f51\u9875\uff0c\u7136\u540e\u8fd4\u56de\u7ed9\u8bf7\u6c42\u8005\u3002JSP\u6280\u672f\u4ee5Java\u8bed\u8a00\u4f5c\u4e3a\u811a\u672c\u8bed\u8a00\uff0c\u4e3a\u7528\u6237\u7684HTTP\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\uff0c\u5e76\u80fd\u4e0e\u670d\u52a1\u5668\u4e0a\u7684\u5176\u5b83Java\u7a0b\u5e8f\u5171\u540c\u5904\u7406\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5f53\u901a\u8fc7HTTP\u53d1\u9001XML\u6570\u636e\u65f6\uff0c\u5c31\u6709\u5fc5\u8981\u4f7f\u7528JSP\u6765\u5904\u7406\u4f20\u5165\u548c\u6d41\u51fa\u7684XML\u6587\u6863\u4e86\uff0c\u6bd4\u5982RSS\u6587\u6863\u3002\u4f5c\u4e3a\u4e00\u4e2aXML\u6587\u6863\uff0c\u5b83\u4ec5\u4ec5\u53ea\u662f\u4e00\u5806\u6587\u672c\u800c\u5df2\uff0c\u4f7f\u7528JSP\u521b\u5efaXML\u6587\u6863\u5e76\u4e0d\u6bd4\u521b\u5efa\u4e00\u4e2aHTML\u6587\u6863\u96be\u3002<\/p>\n

\u4f7f\u7528JSP\u53d1\u9001XML<\/strong><\/div>\n

\u4f7f\u7528JSP\u53d1\u9001XML\u5185\u5bb9\u5c31\u548c\u53d1\u9001HTML\u5185\u5bb9\u4e00\u6837\u3002\u552f\u4e00\u7684\u4e0d\u540c\u5c31\u662f\u60a8\u9700\u8981\u628a\u9875\u9762\u7684context\u5c5e\u6027\u8bbe\u7f6e\u4e3atext\/xml\u3002\u8981\u8bbe\u7f6econtext\u5c5e\u6027\uff0c\u4f7f\u7528<%@page % >\u547d\u4ee4\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n

\r\n<%@ page contentType=\"text\/xml\" %><\/pre>\n

\u63a5\u4e0b\u6765\u8fd9\u4e2a\u4f8b\u5b50\u5411\u6d4f\u89c8\u5668\u53d1\u9001XML\u5185\u5bb9\uff1a<\/p>\n

\r\n<%@ page contentType=\"text\/xml\" %>\r\n\r\n<books>\r\n   <book>\r\n      <name>Padam History<\/name>\r\n      <author>ZARA<\/author>\r\n      <price>100<\/price>\r\n   <\/book>\r\n<\/books><\/pre>\n

\u4f7f\u7528\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u6765\u8bbf\u95ee\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u770b\u770b\u8fd9\u4e2a\u4f8b\u5b50\u6240\u5448\u73b0\u7684\u6587\u6863\u6811\u3002<\/p>\n

\u5728JSP\u4e2d\u5904\u7406XML<\/strong><\/div>\n

\u5728\u4f7f\u7528JSP\u5904\u7406XML\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5c06\u4e0eXML \u548cXPath\u76f8\u5173\u7684\u4e24\u4e2a\u5e93\u6587\u4ef6\u653e\u5728<Tomcat Installation Directory>\\lib\u76ee\u5f55\u4e0b\uff1a<\/p>\n

books.xml\u6587\u4ef6:<\/p>\n

\r\n<books>\r\n<book>\r\n  <name>Padam History<\/name>\r\n  <author>ZARA<\/author>\r\n  <price>100<\/price>\r\n<\/book>\r\n<book>\r\n  <name>Great Mistry<\/name>\r\n  <author>NUHA<\/author>\r\n  <price>2000<\/price>\r\n<\/book>\r\n<\/books><\/pre>\n

main.jsp\u6587\u4ef6\uff1a<\/p>\n

\r\n<%@ page language=\"java\" contentType=\"text\/html; charset=UTF-8\"\r\n    pageEncoding=\"UTF-8\"%>\r\n<%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %>\r\n<%@ taglib prefix=\"x\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/xml\" %>\r\n \r\n<html>\r\n<head>\r\n  <title>JSTL x:parse Tags<\/title>\r\n<\/head>\r\n<body>\r\n<h3>Books Info:<\/h3>\r\n<c:import var=\"bookInfo\" url=\"http:\/\/localhost:8080\/books.xml\"\/>\r\n \r\n<x:parse xml=\"${bookInfo}\" var=\"output\"\/>\r\n<b>The title of the first book is<\/b>: \r\n<x:out select=\"$output\/books\/book[1]\/name\" \/>\r\n<br>\r\n<b>The price of the second book<\/b>: \r\n<x:out select=\"$output\/books\/book[2]\/price\" \/>\r\n \r\n<\/body>\r\n<\/html><\/pre>\n

\u8bbf\u95eehttp:\/\/localhost:8080\/main.jsp\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\r\nBOOKS INFO:\r\nThe title of the first book is:Padam History \r\nThe price of the second book: 2000<\/pre>\n
\u4f7f\u7528JSP\u683c\u5f0f\u5316XML<\/strong><\/div>\n

\u8fd9\u4e2a\u662fXSLT\u6837\u5f0f\u8868style.xsl\u6587\u4ef6\uff1a<\/p>\n

\r\n<?xml version=\"1.0\"?>\r\n<xsl:stylesheet xmlns:xsl=\r\n\"http:\/\/www.w3.org\/1999\/XSL\/Transform\" version=\"1.0\">\r\n \r\n<xsl:output method=\"html\" indent=\"yes\"\/>\r\n \r\n<xsl:template match=\"\/\">\r\n  <html>\r\n  <body>\r\n   <xsl:apply-templates\/>\r\n  <\/body>\r\n  <\/html>\r\n<\/xsl:template>\r\n \r\n<xsl:template match=\"books\">\r\n  <table border=\"1\" width=\"100%\">\r\n    <xsl:for-each select=\"book\">\r\n      <tr>\r\n        <td>\r\n          <i><xsl:value-of select=\"name\"\/><\/i>\r\n        <\/td>\r\n        <td>\r\n          <xsl:value-of select=\"author\"\/>\r\n        <\/td>\r\n        <td>\r\n          <xsl:value-of select=\"price\"\/>\r\n        <\/td>\r\n      <\/tr>\r\n    <\/xsl:for-each>\r\n  <\/table>\r\n<\/xsl:template>\r\n<\/xsl:stylesheet><\/pre>\n

\u8fd9\u4e2a\u662fmain.jsp\u6587\u4ef6\uff1a<\/p>\n

\r\n<%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %>\r\n<%@ taglib prefix=\"x\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/xml\" %>\r\n \r\n<html>\r\n<head>\r\n  <title>JSTL x:transform Tags<\/title>\r\n<\/head>\r\n<body>\r\n<h3>Books Info:<\/h3>\r\n<c:set var=\"xmltext\">\r\n  <books>\r\n    <book>\r\n      <name>Padam History<\/name>\r\n      <author>ZARA<\/author>\r\n      <price>100<\/price>\r\n    <\/book>\r\n    <book>\r\n      <name>Great Mistry<\/name>\r\n      <author>NUHA<\/author>\r\n      <price>2000<\/price>\r\n    <\/book>\r\n  <\/books>\r\n<\/c:set>\r\n \r\n<c:import url=\"http:\/\/localhost:8080\/style.xsl\" var=\"xslt\"\/>\r\n<x:transform xml=\"${xmltext}\" xslt=\"${xslt}\"\/>\r\n \r\n<\/body>\r\n<\/html>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5f53\u901a\u8fc7HTTP\u53d1\u9001XML\u6570\u636e\u65f6\uff0c\u5c31\u6709\u5fc5\u8981\u4f7f\u7528JSP\u6765\u5904\u7406\u4f20\u5165\u548c\u6d41\u51fa\u7684XML\u6587\u6863\u4e86\uff0c\u6bd4\u5982RSS\u6587\u6863\u3002\u4f5c\u4e3a\u4e00\u4e2aXM […]<\/p>\n","protected":false},"author":321,"featured_media":223365,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-223437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=223437"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223437\/revisions"}],"predecessor-version":[{"id":223442,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223437\/revisions\/223442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223365"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=223437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=223437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=223437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}