計(jì)算機(jī)外文翻譯--- xml與jsp聯(lián)手_第1頁(yè)
已閱讀1頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p><b>  英文原文:</b></p><p>  Using XML and JSP together</p><p>  I'm going to assume that, like most Java programmers, you know what JavaServer Pages (JSP) and Extensible Mar

2、kup Language (XML) are, but you may be a little unclear on how you can use them. JSP use is pretty easy to defend. It allows you to design a Website built from files that look and act a lot like HTML. The only difference

3、 is that JSPs also act dynamically -- for example, they can process forms or read databases -- using Java as a server-side scripting language. XML use is more difficult to</p><p>  In this article, you will

4、learn to design a system using XML in a fairly modest way. Many Websites have vast collections of data that are displayed in a more or less standard way. I will design a system that uses XML files to store data on a Web

5、server and JSP files to display that data. </p><p>  XML versus relational databases </p><p>  "But wait," you may ask, "you're using XML to store data? Why not use a database

6、?" Good question. The answer is that for many purposes, a database is overkill. To use a database, you have to install and support a separate server process, which often also requires installing and supporting a dat

7、abase administrator. You must learn SQL, and write SQL queries that convert data from a relational to an object structure and back again. If you store your data as XML files, you lose the overhead of an ex</p><

8、;p>  A more abstract advantage of XML is that, being a hierarchical rather than a relational format, it can be used in a much more straightforward manner to design data structures that fit your needs. You don't ne

9、ed to use an entity relationship editor nor normalize your schema. If you have one element that contains another element, you can represent that directly in the format, rather than using a join table. </p><p&g

10、t;  Note that for many applications, a filesystem will not suffice. If you have a high volume of updates, a filesystem may get confused or corrupted by simultaneous writes; databases usually support transactions, which a

11、llow concurrency without corruption. Further, a database is an excellent tool if you need to make complicated queries, especially if they will vary from time to time. Databases build indexes, and are optimized for keepin

12、g the indexes up to date with a constantly changing data set. Re</p><p>  (Note: You can use simple file locking to provide a poor man's transaction server. And you can also implement an XML index-and-se

13、arch tool in Java, but that's a topic for another article.) </p><p>  In this case, as in most low-to-medium volume, publishing-based Websites, you can assume the following: most of the data access is re

14、ads, not writes; the data, though potentially large, is relatively unchanging; you won't need to do complicated searches, but if you do, you'll use a separate search engine. The advantages of using a mature RDBMS

15、 fade, while the advantage of using an object-oriented data model come to the fore. </p><p>  Finally, it's entirely possible to provide a wrapper for your database that makes SQL queries and translates

16、them into XML streams, so you could have it both ways. XML becomes a more robust, programmer-friendly frontend to a mature database for storing and searching. (Oracle's XSQL servlet is one example of this technique.)

17、 </p><p>  The application: An online photo album </p><p>  Everybody loves photos! People love showing pictures of themselves, their friends, their pets, and their vacations. The Web is the ult

18、imate medium for self-indulgent shutterbugs -- they can annoy their relatives from thousands of miles away. While a full-fledged photo album site would require a complicated object model, I'll focus on defining a sin

19、gle Picture object. (The source code for this application is available in Resources.) The object representing a picture needs fields representing its </p><p>  An image, in turn, needs a few fields of its ow

20、n: the location of the source file (a GIF or JPEG) and the height and width in pixels (to assist you in building <img> tags). Here there is one neat advantage to using the filesystem as your database: you can store

21、 the image files in the same directory as the data files. </p><p>  Finally, let's extend the picture record with an element defining a set of thumbnail images for use in the table of contents or elsewhe

22、re. Here I use the same concept of image I defined earlier. </p><p>  The XML representation of a picture could look something like this: </p><p><b>  <picture></b></p>

23、<p>  <title>Alex On The Beach</title></p><p>  <date>1999-08-08</date></p><p>  <caption>Trying in vain to get a tan</caption></p><p><b

24、>  <image></b></p><p>  <src>alex-beach.jpg</src></p><p>  <width>340</width></p><p>  <height>200</height></p><p>&l

25、t;b>  </image></b></p><p>  <thumbnails></p><p><b>  <image></b></p><p>  <src>alex-beach-sm.jpg</src></p><p>  <w

26、idth>72</width></p><p>  <height>72</height></p><p><b>  </image></b></p><p><b>  <image></b></p><p>  <src&

27、gt;alex-beach-med.jpg</src></p><p>  <width>150</width></p><p>  <height>99</height></p><p><b>  </image></b></p><p>  &

28、lt;/thumbnails></p><p>  </picture></p><p>  Note that by using XML, you put all the information about a single picture into a single file, rather than scattering it among three or four

29、 separate tables. Let's call this a .pix file -- so your filesystem might look like this: </p><p>  summer99/alex-beach.pix</p><p>  summer99/alex-beach.jpg</p><p>  summer99/al

30、ex-beach-sm.jpg</p><p>  summer99/alex-beach-med.jpg</p><p>  summer99/alex-snorkeling.pix</p><p><b>  etc.</b></p><p>  Techniques </p><p>  T

31、here's more than one way to skin a cat, and there's more than one way to bring XML data on to your JSP page. Here is a list of some of those ways. (This list is not exhaustive; many other products and frameworks

32、would serve equally well.) </p><p>  DOM: You can use classes implementing the DOM interface to parse and inspect the XML file </p><p>  XMLEntryList: You can use my code to load the XML into a

33、java.util.List of name-value pairs </p><p>  XPath: You can use an XPath processor (like Resin) to locate elements in the XML file by path name </p><p>  XSL: You can use an XSL processor to tra

34、nsform the XML into HTML </p><p>  Cocoon: You can use the open source Cocoon framework </p><p>  Roll your own bean: You can write a wrapper class that uses one of the other techniques to load

35、the data into a custom JavaBean </p><p>  Note that these techniques could be applied equally well to an XML stream you receive from another source, such as a client or an application server. </p><

36、;p>  JavaServer Pages </p><p>  The JSP spec has had many incarnations, and different JSP products implement different, incompatible versions of the spec. I will use Tomcat, for the following reasons: <

37、;/p><p>  It supports the most up-to-date versions of the JSP and servlet specs </p><p>  It's endorsed by Sun and Apache </p><p>  You can run it standalone without configuring a

38、separate Web server </p><p>  It's open source </p><p>  (For more information on Tomcat, see Resources.) </p><p>  You are welcome to use any JSP engine you like, but configuri

39、ng it is up to you! Be sure that the engine supports at least the JSP 1.0 spec; there were many changes between 0.91 and 1.0. The JSWDK (Java Server Web Development Kit) will work just fine. </p><p>  The JS

40、P structure </p><p>  When building a JSP-driven Website (also known as a Webapp), I prefer to put common functions, imports, constants, and variable declarations in a separate file called init.jsp, located

41、in the source code for this article. </p><p>  I then load that file into each JSP file using <%@include file="init.jsp"%>. The <%@include%> directive acts like the C language's #in

42、clude, pulling in the text of the included file (here, init.jsp) and compiling it as if it were part of the including file (here, picture.jsp). By contrast, the <jsp:include> tag compiles the file as a separate JSP

43、 file and embeds a call to it in the compiled JSP. </p><p>  Finding the file </p><p>  When the JSP starts, the first thing it needs to do after initialization is find the XML file you want. Ho

44、w does it know which of the many files you need? The answer is from a CGI parameter. The user will invoke the JSP with the URL picture.jsp?file=summer99/alex-beach.pix (or by passing a file parameter through an HTML form

45、). </p><p>  However, when the JSP receives the parameter, you're still only halfway there. You still need to know where on the filesystem the root directory lies. For example, on a Unix system, the actu

46、al file may be in the directory /home/alex/public_html/pictures/summer99/alex-beach.pix. JSPs do not have a concept of a current directory while executing, so you need to provide an absolute pathname to the java.io packa

47、ge. </p><p>  The Servlet API provides a method to turn a URL path, relative to the current JSP or Servlet, into an absolute filesystem path. The method ServletContext.getRealPath(String) does the trick. Eve

48、ry JSP has a ServletContext object called application, so the code would be: </p><p>  String picturefile =</p><p>  application.getRealPath("/" + request.getParameter("file"

49、));</p><p><b>  or </b></p><p>  String picturefile =</p><p>  getServletContext().getRealPath("/" + request.getParameter("file"));</p><p&

50、gt;  which also works inside a servlet. (You must append a / because the method expects to be passed the results of request.getPathInfo().) </p><p>  One important note: whenever you access local resources,

51、be very careful to validate the incoming data. A hacker, or a careless user, can send bogus data to hack your site. For instance, consider what would happen if the value file=../../../../etc/passwd were entered. The user

52、 could in this way read your server's password file. </p><p>  The Document Object Model </p><p>  DOM stands for the Document Object Model. It is a standard API for browsing XML documents,

53、developed by the World Wide Web Consortium (W3C). The interfaces are in package org.w3c.dom and are documented at the W3C site (see Resources). </p><p>  There are many DOM parser implementations available.

54、I have chosen IBM's XML4J, but you can use any DOM parser. This is because the DOM is a set of interfaces, not classes -- and all DOM parsers must return objects that faithfully implement those interfaces. </p>

55、<p>  Unfortunately, though standard, the DOM has two major flaws: </p><p>  The API, though object-oriented, is fairly cumbersome. </p><p>  There is no standard API for a DOM parser, so

56、, while each parser returns a org.w3c.dom.Document object, the means of initializing the parser and loading the file itself is always parser specific. </p><p>  The simple picture file described above is rep

57、resented in the DOM by several objects in a tree structure. </p><p>  Document Node</p><p>  --> Element Node "picture"</p><p>  --> Text Node "\n " (whit

58、espace)</p><p>  --> Element Node "title"</p><p>  --> Text Node "Alex On The Beach"</p><p>  --> Element Node "date"</p><p>  --

59、> ... etc.</p><p>  To acquire the value Alex On The Beach you would have to make several method calls, walking the DOM tree. Further, the parser may choose to intersperse any number of whitespace text no

60、des, through which you would have to loop and either ignore or concatenate (you can correct this by calling the normalize() method). The parser may also include separate nodes for XML entities (like &amp;), CDATA nod

61、es, or other element nodes (for instance, the <b>big<b> bear would turn into at least three nodes, o</p><p>  From a higher perspective, the problem with DOM is that the XML objects are not avail

62、able directly as Java objects, but they must be accessed piecemeal via the DOM API. See my conclusion for a discussion of Java-XML Data Binding technology, which uses this straight-to-Java approach for accessing XML data

63、. </p><p>  I have written a small utility class, called DOMUtils, that contains static methods for performing common DOM tasks. For instance, to acquire the text content of the title child element of the ro

64、ot (picture) element, you would write the following code: </p><p>  Document doc = DOMUtils.xml4jParse(picturefile);</p><p>  Element nodeRoot = doc.getDocumentElement();</p><p>  N

65、ode nodeTitle = DOMUtils.getChild(nodeRoot, "title");</p><p>  String title = (nodeTitle == null) ? null : DOMUtils.getTextValue(nodeTitle);</p><p><b>  中文翻譯:</b></p>

66、<p><b>  XML與JSP聯(lián)手</b></p><p>  我們?cè)诖思僭O(shè)你已經(jīng)了解javaserver pages(jsp)和extensible markup language (xml)。但也許你對(duì)該如何綜合使用它們?nèi)匀挥行┟曰?。jsp的應(yīng)用很容易,你可以用它設(shè)計(jì)網(wǎng)頁(yè),使之看起來(lái)似乎和html一樣。唯一的不同是jsp是動(dòng)態(tài)執(zhí)行的。例如,它們可以處理表單form和讀寫(xiě)

67、數(shù)據(jù)庫(kù)。xml的應(yīng)用的說(shuō)明則比較困難。似乎所有的產(chǎn)品都支持它,每個(gè)人也好象都以各種不同目的在使用它。 </p><p>  在本文中,你可以看到如何使用一種相當(dāng)先進(jìn)的方式用xml來(lái)設(shè)計(jì)一個(gè)系統(tǒng)。許多站點(diǎn)有巨量數(shù)據(jù)收集并以一種很標(biāo)準(zhǔn)或很不標(biāo)準(zhǔn)的方式來(lái)顯示它們。我將設(shè)計(jì)一個(gè)系統(tǒng),它使用xml文件在web服務(wù)器上進(jìn)行存儲(chǔ),并用jsp來(lái)顯示數(shù)據(jù)。</p><p>  xml vs 關(guān)系型數(shù)據(jù)庫(kù) &

68、lt;/p><p>  "等一下!"你可能問(wèn),"你用xml文件存儲(chǔ)數(shù)據(jù)嗎?為什么不使用數(shù)據(jù)庫(kù)?" </p><p>  這個(gè)問(wèn)題問(wèn)的很好。我的回答是,對(duì)很多目的用途來(lái)說(shuō),用數(shù)據(jù)庫(kù)太過(guò)浪費(fèi)了。.要使用一個(gè)數(shù)據(jù)庫(kù),你必須安裝和支持一個(gè)分離的服務(wù)器處理進(jìn)程(a separate server process),它常要求有安裝和支持它的administrator

69、。你必須學(xué)習(xí)sql, 并用sql寫(xiě)查詢,然后轉(zhuǎn)換數(shù)據(jù),再返回。而如果你用xml文件存儲(chǔ)數(shù)據(jù),將可減少額外的服務(wù)器的負(fù)荷。還有,你還找到了一個(gè)編輯數(shù)據(jù)的簡(jiǎn)單方法。你只要使用文本編輯器,而不必使用復(fù)雜的數(shù)據(jù)庫(kù)工具。xml文件很容易備份,和朋友共享,或下載到你的客戶端。同樣的,你可以方便地通過(guò)ftp上載新的數(shù)據(jù)到你的站點(diǎn)。 </p><p>  xml還有一個(gè)更抽象的優(yōu)點(diǎn),即作為層次型的格式比關(guān)系型的更好。 它可以用一

70、種很直接的方式來(lái)設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)來(lái)符合你的需要。你不需要使用一個(gè)實(shí)體-關(guān)系編輯器,也不需要使你的圖表(schema)標(biāo)準(zhǔn)化。 如果你有一個(gè)元素(element)包含了另一個(gè)元素,你可以直接在格式中表示它,而不需要使用表的關(guān)聯(lián)。 </p><p>  注意,在很多應(yīng)用中,依靠文件系統(tǒng)是不夠充分的。如果更新很多,文件系統(tǒng)會(huì)因?yàn)橥瑫r(shí)寫(xiě)入而受到破壞。數(shù)據(jù)庫(kù)則通常支持事務(wù)處理,可以應(yīng)付所發(fā)生的請(qǐng)求而不至于損壞。對(duì)于復(fù)雜的查詢統(tǒng)

71、計(jì)要有反復(fù)、及時(shí)的更新,此時(shí)數(shù)據(jù)庫(kù)表現(xiàn)都很優(yōu)秀。當(dāng)然,關(guān)系型數(shù)據(jù)庫(kù)還有很多優(yōu)點(diǎn),包括豐富的查詢語(yǔ)言,圖表化工具,可伸縮性,存取控制等等。 </p><p>  (注意:你可以使用簡(jiǎn)單的文件鎖定來(lái)提供一個(gè)事務(wù)處理服務(wù)器,你還可以在java中執(zhí)行一種 xml index-and-search工具,不過(guò)這已經(jīng)是另外一篇文章的主題了。) </p><p>  在下面這樣的案例中,正如大多數(shù)中小規(guī)模

72、的、基于發(fā)布信息的站點(diǎn)一樣,你可能涉及的大多數(shù)數(shù)據(jù)存取都是讀,而不是寫(xiě),數(shù)據(jù)雖然可能很大,但相對(duì)來(lái)說(shuō)并沒(méi)有經(jīng)常的更新變化,你也不需要做很復(fù)雜的查詢,即使你需要做,也將用一個(gè)獨(dú)立的查詢工具,那么成熟的rdbms的優(yōu)點(diǎn)消失了,而面向?qū)ο笮偷臄?shù)據(jù)模型的優(yōu)點(diǎn)則可以得到體現(xiàn)。 </p><p>  最后,為你的數(shù)據(jù)庫(kù)提供一個(gè)查詢器外殼來(lái)進(jìn)行sql查詢并將他們轉(zhuǎn)化進(jìn)入xml stream也是完全有可能的。 </p>

73、;<p>  所以你可以選擇這二種方式之一。xml正變成一種非常健壯的,便于編程的工具,作為某個(gè)成熟的數(shù)據(jù)庫(kù)的前端工具來(lái)進(jìn)行存儲(chǔ)和查詢。(oracle的xsql servlet即是這種技術(shù)的一個(gè)很好的例子。) </p><p>  所有人都喜歡照相!他們喜歡展示自己的,親人的,朋友的,度假時(shí)的照片,而 web 是他們展示的好地方。-- 即使千里之外的親戚都可以看到。我將著重于定義一個(gè)單獨(dú)的pictu

74、re對(duì)象。(這一應(yīng)用的源代碼在resources中可以取得) 。該對(duì)象描述了表示一張照片所需要的字段:title,date,一個(gè)可選的標(biāo)題,以及對(duì)圖片來(lái)源的一個(gè)指向。 </p><p>  一個(gè)圖象,需要它自己的一些字段:源文件( gif/jpeg)的定位,寬度和高度像素(以協(xié)助建立<img> 標(biāo)記。 這里可以看到一個(gè)很簡(jiǎn)單的優(yōu)點(diǎn),即使用文件系統(tǒng)來(lái)代替數(shù)據(jù)庫(kù)的時(shí)候,你可以將圖形文件存放在與數(shù)據(jù)文件相同

75、的目錄中。 </p><p>  最后,讓我們來(lái)用一個(gè)元素?cái)U(kuò)展圖片記錄,該元素定義了一套縮略圖(thumbnail)來(lái)用于內(nèi)容表或其它地方。這里我用了和先前同樣定義的圖片內(nèi)容。</p><p>  一張圖片的xml表示可以是這樣的: </p><p><b>  <picture></b></p><p> 

76、 <title>alex on the beach</title></p><p>  <date>1999-08-08</date></p><p>  <caption>trying in vain to get a tan</caption></p><p><b>  <

77、image></b></p><p>  <src>alex-beach.jpg</src></p><p>  <width>340</width></p><p>  <height>200</height></p><p><b>  &

78、lt;/image></b></p><p>  <thumbnails></p><p><b>  <image></b></p><p>  <src>alex-beach-sm.jpg</src></p><p>  <width>72

79、</width></p><p>  <height>72</height></p><p><b>  </image></b></p><p><b>  <image></b></p><p>  <src>alex-be

80、ach-med.jpg</src></p><p>  <width>150</width></p><p>  <height>99</height></p><p><b>  </image></b></p><p>  </thumbn

81、ails></p><p>  </picture></p><p>  注意,通過(guò)使用xml, 你將一張單獨(dú)圖片的全部信息放到了一個(gè)單獨(dú)的文件中,而不是將它分散放入3-4個(gè)表中。</p><p>  我們將這稱為 .pix file </p><p>  -- 于是你的文件系統(tǒng)會(huì)是這樣的: </p><

82、p>  summer99/alex-beach.pix</p><p>  summer99/alex-beach.jpg</p><p>  summer99/alex-beach-sm.jpg</p><p>  summer99/alex-beach-med.jpg</p><p>  summer99/alex-snorkeli

83、ng.pix</p><p><b>  etc.</b></p><p><b>  技術(shù)篇</b></p><p>  俗話說(shuō),要?jiǎng)兿仑埖钠さ姆椒ê沃挂环N。同樣,將xml數(shù)據(jù)放到j(luò)sp中也不止一種辦法。這里列舉了其中一些方法,(其實(shí),很多其它工具也可以做得同樣出色。) </p><p>  do

84、m: 你可以使用類(lèi)(classes)來(lái)調(diào)用dom接口(interface)對(duì)xml文件進(jìn)行分析檢查。 </p><p>  xmlentrylist: 你可以使用我的代碼來(lái)將xml加載到name-value pairs 的java.util.list中。 </p><p>  xpath: 你可以使用一個(gè) xpath處理器(如resin)通過(guò)路徑名在xml文件中定位元素。</p>

85、;<p>  xsl:你可以使用某種xsl處理器將xml轉(zhuǎn)換成為html。 </p><p>  cocoon: 你可以使用開(kāi)放源碼的cocoon framework 。</p><p>  運(yùn)行你自己的bean: 你可以寫(xiě)一個(gè)外殼類(lèi)(wrapper class),使用某種其它技術(shù)來(lái)將數(shù)據(jù)加載到字定義的javabean中。 </p><p>  請(qǐng)注意

86、這些技術(shù)將和一個(gè)你從另外來(lái)源取得的xml stream執(zhí)行得同樣出色,例如一個(gè)客戶端或者一個(gè)應(yīng)用服務(wù)器。</p><p>  javaserver pages</p><p>  jsp規(guī)范有很多替身,不同的jsp產(chǎn)品表現(xiàn)也不盡相同,不同版本之間也有差別。我選擇了tomcat,這基于以下原因: </p><p>  它支持大多數(shù)最新的jsp/servlet規(guī)范 &l

87、t;/p><p>  它受到 sun和apache認(rèn)同 </p><p>  你可以獨(dú)立運(yùn)行它而不需要另外配置一個(gè)web服務(wù)器。 </p><p><b>  它是開(kāi)放源碼的</b></p><p>  你可以選擇任何你喜歡的jsp引擎,但要自己配置它,它必須至少支持jsp 1.0規(guī)范。0.91和1.0之間有了許多區(qū)別。而j

88、swdk (java server web development kit) 可能剛剛好地適合要求</p><p><b>  jsp結(jié)構(gòu)</b></p><p>  當(dāng)創(chuàng)建一個(gè)jsp網(wǎng)站 (webapp), 我喜歡將公用的函數(shù)、導(dǎo)入、常量、變量聲明都放入到一個(gè)單獨(dú)的文件init.jsp中。 然后用 <%@include file="init.jsp&

89、quot;%>加載到每一個(gè)文件中去。 <%@include%> 就象c語(yǔ)言的 #include, include在編譯時(shí)使其中的文本作為一個(gè)部分被加入并一起進(jìn)行編譯,相對(duì)地, <jsp:include>標(biāo)記則是使其中的文件被獨(dú)立地進(jìn)行編譯,然后在文件中嵌入一個(gè)對(duì)它的調(diào)用。 </p><p><b>  查找文件</b></p><p> 

90、 當(dāng)jsp啟動(dòng)時(shí),初始化后第一件事情就是查找你要的xml文件。它是怎么知道在眾多文件中你要找的是哪一個(gè)? 它來(lái)自與一個(gè)參數(shù),使用者會(huì)在調(diào)用jsp的url中加入?yún)?shù): picture.jsp?file=summer99/alex-beach.pix (或者通過(guò)html表單來(lái)傳遞文件參數(shù))。 </p><p>  但是,當(dāng)jsp接受此參數(shù)以后,你仍然只完成了一半工作,因?yàn)檫€要知道文件系統(tǒng)的根目錄在哪里。例如,在uni

91、x系統(tǒng)中,實(shí)際文件可能在這樣的路徑:</p><p>  /home/alex/public_html/pictures/summer99/alex-beach.pix。</p><p>  jsp文件在執(zhí)行狀態(tài)時(shí)沒(méi)有當(dāng)前路徑概念。所以你為java.io包要給出一個(gè)絕對(duì)路徑。</p><p>  servlet api可以提供一個(gè)方法來(lái)將一個(gè)url路徑,從相對(duì)于當(dāng)前

92、jsp或servlet的路徑轉(zhuǎn)化成為一個(gè)絕對(duì)的文件系統(tǒng)路徑。方法是:</p><p>  servletcontext.getrealpath(string)。</p><p>  每一個(gè)jsp有一個(gè)叫做application的 servletcontext對(duì)象。所以代碼可以是: </p><p>  string picturefile =</p>

93、<p>  application.getrealpath("/" + request.getparameter("file"));</p><p><b>  或者 </b></p><p>  string picturefile =</p><p>  getservletcontext(

94、).getrealpath("/" + request.getparameter("file"));</p><p>  它也可以在servlet中工作。(你必須加上 / 因?yàn)榇朔椒ㄐ枰獋鬟frequest.getpathinfo()的結(jié)果。) </p><p>  這里有一個(gè)重要的提示:每當(dāng)你存取本地的資源,要非常小心地檢查輸入數(shù)據(jù)的合法性。黑客或者

95、粗心的用戶,可能發(fā)送偽造的或錯(cuò)誤的數(shù)據(jù)來(lái)破壞你的站點(diǎn)。例如,請(qǐng)想一下以下的表達(dá)會(huì)發(fā)生什么結(jié)果:</p><p>  如果輸入了file=../../../../etc/passwd。這樣用戶回讀到你的服務(wù)器的password文件! </p><p>  dom (document object model)</p><p>  dom 代表文檔對(duì)象模型documen

96、t object model。它是瀏覽xml文檔的一種標(biāo)準(zhǔn)api,由world wide web consortium (w3c)發(fā)展。 接口在org.w3c.dom包中,文檔參見(jiàn)w3c站點(diǎn)。</p><p>  有許多可用的dom分析器工具。我選擇了 ibm的xml4j。但你可以使用任何其它的dom分析器。這是因?yàn)閐om是一套接口,而不是類(lèi) --所有的dom分析器(parser)必須返回同樣地處理這些接口的對(duì)象

97、。</p><p>  遺憾的是,雖然很標(biāo)準(zhǔn),dom還是有兩大缺陷: </p><p>  1 api雖然也是面向?qū)ο蟮模€是相當(dāng)笨重。 </p><p>  dom parser并沒(méi)有標(biāo)準(zhǔn)的api,所以, 當(dāng)每一個(gè)分析器返回一個(gè)org.w3c.dom對(duì)象,文檔對(duì)象--分析器初始化和文件自身加載的方式,對(duì)應(yīng)于不同分析器通??偸翘囟ǖ?。 </p><

98、;p>  這個(gè)簡(jiǎn)單的上面已描述的圖片文件在dom中可以在一個(gè)樹(shù)結(jié)構(gòu)中通過(guò)一些對(duì)象表示如下: </p><p>  document node</p><p>  --> element node "picture"</p><p>  --> text node "\n " (whitespace)</

99、p><p>  --> element node "title"</p><p>  --> text node "alex on the beach"</p><p>  --> element node "date"</p><p>  --> ... etc

100、.</p><p>  為了取得“alex on the beach”,你要做一些方法調(diào)用,游歷dom樹(shù),而且,分析器可能選擇分散“whitespace”文本nodes的一些數(shù)據(jù),你不得不使用循環(huán)和串聯(lián)等 (你可以通過(guò)調(diào)用normalize()來(lái)糾正這個(gè)問(wèn)題。)分析器可能還包含了分離的xml實(shí)體(如 &amp;), cdata nodes或者其它實(shí)體nodes (如<b>big<b>

101、;會(huì)被變成至少三個(gè)node。也沒(méi)有辦法在dom中簡(jiǎn)單表示"get me the text value of the title element." 總之,在dom中游歷有一點(diǎn)笨重。(參見(jiàn)本文用xpath取代dom章節(jié)。) </p><p>  2 從更高處看,dom的問(wèn)題是xml對(duì)象無(wú)法象java對(duì)象一樣可以直接得到,它們需要通過(guò) dom api一個(gè)一個(gè)地得到。</p><

102、p>  你可以參考我的為java-xml data binding技術(shù)討論做的一些歸納,那里也用了這種直接使用java的方法來(lái)存取xml數(shù)據(jù)。 </p><p>  我寫(xiě)了一個(gè)小的工具類(lèi),叫做domutils,包含了靜態(tài)方法來(lái)執(zhí)行公用的dom任務(wù)。例如,要獲得根(圖片)元素的title子元素的文本內(nèi)容,你可以編寫(xiě)如下代碼:</p><p>  document doc = domut

103、ils.xml4jparse(picturefile);</p><p>  element noderoot = doc.getdocumentelement();</p><p>  node nodetitle = domutils.getchild(noderoot, "title");</p><p>  string title =

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論