📄 introxml4.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Generating XML Data</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="IntroXML3.html" /> <link rel="Next" href="IntroXML5.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <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="IntroXML3.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="IntroXML5.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"> <blockquote><a name="wp67593"> </a><h2 class="pHeading1">Generating XML Data</h2><a name="wp67604"> </a><p class="pBody">This section also takes you step by step through the process of constructing an XML document. Along the way, you'll gain experience with the XML components you'll typically use to create your data structures.</p><a name="wp67589"> </a><h3 class="pHeading2">Writing a Simple XML File</h3><a name="wp67569"> </a><p class="pBody">You'll start by writing the kind of XML data you could use for a slide presentation. In this exercise, you'll use your text editor to create the data in order to become comfortable with the basic format of an XML file. You'll be using this file and extending it in later exercises. </p><a name="wp67399"> </a><h4 class="pHeading3">Creating the File</h4><a name="wp67400"> </a><p class="pBody">Using a standard text editor, create a file called <code class="cCode">slideSample.xml</code>.</p><hr><a name="wp67402"> </a><p class="pNote">Note: Here is a version of it that already exists: <code class="cCode"><a href="../examples/xml/samples/slideSample01.xml" target="_blank">slideSample01.xml</a></code>. (The browsable version is<code class="cCode"><a href="../examples/xml/samples/slideSample01-xml.html" target="_blank"> slideSample01-xml.html</a></code>.) You can use this version to compare your work, or just review it as you read this guide.</p><hr><a name="wp67406"> </a><h4 class="pHeading3">Writing the Declaration</h4><a name="wp67407"> </a><p class="pBody">Next, write the declaration, which identifies the file as an XML document. The declaration starts with the characters <code class="cCode">"<?"</code>, which is the standard XML identifier for a <span style="font-style: italic">processing instruction</span>. (You'll see other processing instructions later on in this tutorial.)</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> <?xml version='1.0' encoding='utf-8'?> <a name="wp67408"> </a></pre></div><a name="wp67409"> </a><p class="pBody">This line identifies the document as an XML document that conforms to version 1.0 of the XML specification, and says that it uses the 8-bit Unicode character-encoding scheme. (For information on encoding schemes, see <a href="Encodings.html#wp64176">Java Encoding Schemes</a>.)</p><a name="wp67413"> </a><p class="pBody">Since the document has not been specified as "standalone", the parser assumes that it may contain references to other documents. To see how to specify a document as "standalone", see <a href="IntroXML2.html#wp63920">The XML Prolog</a>.</p><a name="wp67420"> </a><h4 class="pHeading3">Adding a Comment</h4><a name="wp67421"> </a><p class="pBody">Comments are ignored by XML parsers. A program will never see them in fact, unless you activate special settings in the parser. Add the text highlighted below to put a comment into the file.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version='1.0' encoding='utf-8'?> <code class="cCodeBold"><!-- A SAMPLE set of slides --></code> <a name="wp67425"> </a></pre></div><a name="wp67429"> </a><h3 class="pHeading2">Defining the Root Element</h3><a name="wp67430"> </a><p class="pBody">After the declaration, every XML file defines exactly one element, known as the root element. Any other elements in the file are contained within that element. Enter the text highlighted below to define the root element for this file, <code class="cCode">slideshow</code>: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version='1.0' encoding='utf-8'?> <!-- A SAMPLE set of slides --> <code class="cCodeBold"><slideshow> </slideshow></code><a name="wp67431"> </a></pre></div><hr><a name="wp67432"> </a><p class="pNote">Note: XML element names are case-sensitive. The end-tag must exactly match the start-tag.</p><hr><a name="wp67436"> </a><h4 class="pHeading3">Adding Attributes to an Element</h4><a name="wp67437"> </a><p class="pBody">A slide presentation has a number of associated data items, none of which require any structure. So it is natural to define them as attributes of the <code class="cCode">slideshow</code> element. Add the text highlighted below to set up some attributes:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">... <slideshow <code class="cCodeBold">title="Sample Slide Show" date="Date of publication" author="Yours Truly"</code> > </slideshow><a name="wp67438"> </a></pre></div><a name="wp67439"> </a><p class="pBody">When you create a name for a tag or an attribute, you can use hyphens (<code class="cCode">"-"</code>), underscores (<code class="cCode">"_"</code>), colons (<code class="cCode">":"</code>), and periods (<code class="cCode">"."</code>) in addition to characters and numbers. Unlike HTML, values for XML attributes are always in quotation marks, and multiple attributes are never separated by commas. </p><hr><a name="wp67440"> </a><p class="pNote">Note: Colons should be used with care or avoided altogether, because they are used when defining the namespace for an XML document.</p><hr><a name="wp67443"> </a><h4 class="pHeading3">Adding Nested Elements</h4><a name="wp67444"> </a><p class="pBody">XML allows for hierarchically structured data, which means that an element can contain other elements. Add the text highlighted below to define a slide element and a title element contained within it:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><slideshow ... ><code class="cCodeBold"> <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide></code></slideshow><a name="wp67445"> </a></pre></div><a name="wp67446"> </a><p class="pBody">Here you have also added a <code class="cCode">type</code> attribute to the slide. The idea of this attribute is that slides could be earmarked for a mostly technical or mostly executive audience with <code class="cCode">type="tech"</code> or <code class="cCode">type="exec"</code>, or identified as suitable for both with <code class="cCode">type="all"</code>. </p><a name="wp67448"> </a><p class="pBody">More importantly, though, this example illustrates the difference between things that are more usefully defined as elements (the <code class="cCode">title</code> element) and things that are more suitable as attributes (the <code class="cCode">type</code> attribute). The visibility heuristic is primarily at work here. The title is something the audience will see. So it is an element. The type, on the other hand, is something that never gets presented, so it is an attribute. Another way to think about that distinction is that an element is a container, like a bottle. The type is a characteristic of the <span style="font-style: italic">container</span> (is it tall or short, wide or narrow). The title is a characteristic of the <span style="font-style: italic">contents</span> (water, milk, or tea). These are not hard and fast rules, of course, but they can help when you design your own XML structures.</p><a name="wp67450"> </a><h4 class="pHeading3">Adding HTML-Style Text</h4><a name="wp67451"> </a><p class="pBody">Since XML lets you define any tags you want, it makes sense to define a set of tags that look like HTML. The XHTML standard does exactly that, in fact. You'll see more about that towards the end of the SAX tutorial. For now, type the text highlighted below to define a slide with a couple of list item entries that use an HTML-style <code class="cCode"><em></code> tag for emphasis (usually rendered as italicized text): </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> ... <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide><code class="cCodeBold"> <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <item>Who <em>buys</em> WonderWidgets</item> </slide></code></slideshow><a name="wp67453"> </a></pre></div><a name="wp67454"> </a><p class="pBody">Note that defining a <span style="font-style: italic">title</span> element conflicts with the XHTML element that uses the same name. We'll discuss the mechanism that produces the conflict (the DTD), along with possible solutions, later on in this tutorial.</p><a name="wp67460"> </a><h4 class="pHeading3">Adding an Empty Element</h4><a name="wp67461"> </a><p class="pBody">One major difference between HTML and XML, though, is that all XML must be <span style="font-style: italic">well-formed</span> -- which means that every tag must have an ending tag or be an empty tag. You're getting pretty comfortable with ending tags, by now. Add the text highlighted below to define an empty list item element with no contents:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> ... <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item><code class="cCodeBold"> <item/></code> <item>Who <em>buys</em> WonderWidgets</item> </slide></slideshow><a name="wp67463"> </a></pre></div><a name="wp67464"> </a><p class="pBody">Note that any element can be empty element. All it takes is ending the tag with <code class="cCode">"/>"</code> instead of <code class="cCode">">"</code>. You could do the same thing by entering <code class="cCode"><item></item></code>, which is equivalent. </p><hr><a name="wp67466"> </a><p class="pNote">Note: Another factor that makes an XML file <span style="font-style: italic">well-formed</span> is proper nesting. So <code class="cCode"><b><i>some_text</i></b></code> is well-formed, because the <code class="cCode"><i>...</i></code> sequence is completely nested within the <code class="cCode"><b>..</b></code> tag. This sequence, on the other hand, is not well-formed: <code class="cCode"><b><i>some_text</b></i></code>. </p><hr><a name="wp67468"> </a><h4 class="pHeading3">The Finished Product</h4><a name="wp67469"> </a><p class="pBody">Here is the completed version of the XML file:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -