📄 introxml2.html
字号:
<a name="wp63968"> </a><p class="pBody">One of the nicer aspects of XML documents is that they can be composed from separate entities. You can do that with HTML, but only by linking to other documents. Unlike HTML, XML entities can be included "in line" in a document. The included sections look like a normal part of the document--you can search the whole document at one time or download it in one piece. That lets you modularize your documents without resorting to links. You can single-source a section so that an edit to it is reflected everywhere the section is used, and yet a document composed from such pieces looks for all the world like a one-piece document. </p><a name="wp63970"> </a><h4 class="pHeading3">Linkability</h4><a name="wp63971"> </a><p class="pBody">Thanks to HTML, the ability to define links between documents is now regarded as a necessity. The next section of this tutorial, <a href="IntroXML3.html#wp64046">XML and Related Specs: Digesting the Alphabet Soup</a>, discusses the link-specification initiative. This initiative lets you define two-way links, multiple-target links, "expanding" links (where clicking a link causes the targeted information to appear inline), and links between two existing documents that are defined in a third. </p><a name="wp63975"> </a><h4 class="pHeading3">Easily Processed</h4><a name="wp63976"> </a><p class="pBody">As mentioned earlier, regular and consistent notation makes it easier to build a program to process XML data. For example, in HTML a <code class="cCode"><dt></code> tag can be delimited by <code class="cCode"></dt></code>, another <code class="cCode"><dt></code>, <code class="cCode"><dd></code>, or <code class="cCode"></dl></code>. That makes for some difficult programming. But in XML, the <code class="cCode"><dt></code> tag must always have a <code class="cCode"></dt></code> terminator, or else it will be defined as a <code class="cCode"><dt/></code> tag. That restriction is a critical part of the constraints that make an XML document well-formed. (Otherwise, the XML parser won't be able to read the data.) And since XML is a vendor-neutral standard, you can choose among several XML parsers, any one of which takes the work out of processing XML data.</p><a name="wp63977"> </a><h4 class="pHeading3">Hierarchical</h4><a name="wp63978"> </a><p class="pBody">Finally, XML documents benefit from their hierarchical structure. Hierarchical document structures are, in general, faster to access because you can drill down to the part you need, like stepping through a table of contents. They are also easier to rearrange, because each piece is delimited. In a document, for example, you could move a heading to a new location and drag everything under it along with the heading, instead of having to page down to make a selection, cut, and then paste the selection into a new location.</p><a name="wp63980"> </a><h3 class="pHeading2">How Can You Use XML?</h3><a name="wp63981"> </a><p class="pBody"> There are several basic ways to make use of XML:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp63982"> </a><div class="pSmartList1"><li>Traditional data processing, where XML encodes the data for a program to process</li></div><a name="wp63983"> </a><div class="pSmartList1"><li>Document-driven programming, where XML documents are containers that build interfaces and applications from existing components</li></div><a name="wp63984"> </a><div class="pSmartList1"><li>Archiving--the foundation for document-driven programming, where the customized version of a component is saved (archived) so it can be used later</li></div><a name="wp63985"> </a><div class="pSmartList1"><li>Binding, where the DTD or schema that defines an XML data structure is used to automatically generate a significant portion of the application that will eventually process that data</li></div></ul></div><a name="wp63987"> </a><h4 class="pHeading3">Traditional Data Processing</h4><a name="wp63988"> </a><p class="pBody">XML is fast becoming the data representation of choice for the Web. It's terrific when used in conjunction with network-centric Java-platform programs that send and retrieve information. So a client/server application, for example, could transmit XML-encoded data back and forth between the client and the server. </p><a name="wp63989"> </a><p class="pBody">In the future, XML is potentially the answer for data interchange in all sorts of transactions, as long as both sides agree on the markup to use. (For example, should an e-mail program expect to see tags named <code class="cCode"><FIRST></code> and <code class="cCode"><LAST></code>, or <code class="cCode"><FIRSTNAME></code> and <code class="cCode"><LASTNAME></code>) The need for common standards will generate a lot of industry-specific standardization efforts in the years ahead. In the meantime, mechanisms that let you "translate" the tags in an XML document will be important. Such mechanisms include projects like the <a href="IntroXML3.html#wp64220">RDF</a> initiative, which defines "meta tags", and the <a href="IntroXML3.html#wp64148">XSL</a> specification, which lets you translate XML tags into other XML tags. </p><a name="wp63997"> </a><h4 class="pHeading3">Document-Driven Programming</h4><a name="wp63998"> </a><p class="pBody">The newest approach to using XML is to construct a document that describes how an application page should look. The document, rather than simply being displayed, consists of references to user interface components and business-logic components that are "hooked together" to create an application on the fly.</p><a name="wp63999"> </a><p class="pBody">Of course, it makes sense to utilize the Java platform for such components. Both Java Beans components for interfaces and Enterprise Java Beans components for business logic can be used to construct such applications. Although none of the efforts undertaken so far are ready for commercial use, much preliminary work has already been done.</p><hr><a name="wp64000"> </a><p class="pNote">Note: The Java programming language is also excellent for writing XML-processing tools that are as portable as XML. Several Visual XML editors have been written for the Java platform. For a listing of editors, processing tools, and other XML resources, see the "Software" section of Robin Cover's SGML/XML Web Page at <code class="cCode"><a href="http://www.oasis-open.org/cover/" target="_blank">http://www.oasis-open.org/cover/</a></code>. </p><hr><a name="wp64002"> </a><h4 class="pHeading3">Binding</h4><a name="wp64003"> </a><p class="pBody">Once you have defined the structure of XML data using either a DTD or the one of the schema standards, a large part of the processing you need to do has already been defined. For example, if the schema says that the text data in a <code class="cCode"><date></code> element must follow one of the recognized date formats, then one aspect of the validation criteria for the data has been defined--it only remains to write the code. Although a DTD specification cannot go the same level of detail, a DTD (like a schema) provides a grammar that tells which data structures can occur, in what sequences. That specification tells you how to write the high-level code that processes the data elements.</p><a name="wp64004"> </a><p class="pBody">But when the data structure (and possibly format) is fully specified, the code you need to process it can just as easily be generated automatically. That process is known as <span style="font-style: italic">binding</span>--creating classes that recognize and process different data elements by processing the specification that defines those elements. As time goes on, you should find that you are using the data specification to generate significant chunks of code, so you can focus on the programming that is unique to your application.</p><a name="wp64006"> </a><h4 class="pHeading3">Archiving</h4><a name="wp64007"> </a><p class="pBody">The Holy Grail of programming is the construction of reusable, modular components. Ideally, you'd like to take them off the shelf, customize them, and plug them together to construct an application, with a bare minimum of additional coding and additional compilation.</p><a name="wp64008"> </a><p class="pBody">The basic mechanism for saving information is called <span style="font-style: italic">archiving</span>. You archive a component by writing it to an output stream in a form that you can reuse later. You can then read it in and instantiate it using its saved parameters. (For example, if you saved a table component, its parameters might be the number of rows and columns to display.) Archived components can also be shuffled around the Web and used in a variety of ways. </p><a name="wp64009"> </a><p class="pBody">When components are archived in binary form, however, there are some limitations on the kinds of changes you can make to the underlying classes if you want to retain compatibility with previously saved versions. If you could modify the archived version to reflect the change, that would solve the problem. But that's hard to do with a binary object. Such considerations have prompted a number of investigations into using XML for archiving. But if an object's state were archived in text form using XML, then anything and everything in it could be changed as easily as you can say, "search and replace". </p><a name="wp64010"> </a><p class="pBody">XML's text-based format could also make it easier to transfer objects between applications written in different languages. For all of these reasons, XML-based archiving is likely to become an important force in the not-too-distant future.</p><a name="wp64011"> </a><h4 class="pHeading3">Summary</h4><a name="wp64012"> </a><p class="pBody">XML is pretty simple, and very flexible. It has many uses yet to be discovered--we are just beginning to scratch the surface of its potential. It is the foundation for a great many standards yet to come, providing a common language that different computer systems can use to exchange data with one another. As each industry-group comes up with standards for what they want to say, computers will begin to link to each other in ways previously unimaginable. </p><a name="wp64013"> </a><p class="pBody">For more information on the background and motivation of XML, see this great article in Scientific American at</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><a href="http://www.sciam.com/1999/0599issue/0599bosak.html" target="_blank">http://www.sciam.com/1999/0599issue/0599bosak.html</a><a name="wp64014"> </a></pre></div> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="IntroXML.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="IntroXML3.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -