⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1_write.html

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><!--NewPage--><html><head><title>Writing a Simple XML File</title><style type="text/css"><!----></style></head><body BGCOLOR="#ffffff"><table width="100%"><tr>    <td align=left> <a href="index.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><ahref="2a_echo.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=bottom border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=bottom border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=bottom border=0 alt="Top | "></a>     </td><td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a></td></tr></table><p><center>    <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202"    HEIGHT="25" NATURALSIZEFLAG="3"> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202"    HEIGHT="25" NATURALSIZEFLAG="3">   </center>  <blockquote>      <blockquote>         <hr size=4>     </blockquote>  </blockquote><h2>2a. Writing a Simple XML File</h2><table width="40%" border="1" align="right">  <tr>     <td>       <div align="center"><b><i>Link Summary</i></b></div>    </td>  </tr>  <tr>     <td height="256">       <dl>         <dt><b><i>Local Links</i></b></dt>      </dl>      <ul>        <li><a href="../overview/1_xml.html">A Quick Introduction to XML</a></li>        <li><a href="../overview/1_xml.html#prolog">The XML Prolog</a></li>        <li><a href="8_lex.html">Using a LexicalEventListener</a></li>        <li><a href="7b_pe.html">Parsing the Parameterized DTD</a></li>      </ul>      <dl>        <dt><b><i>Exercise Links</i></b></dt>      </dl>      <ul>        <li><a href="samples/slideSample01.xml">slideSample01.xml</a></li>      </ul>      <p><b><i>Glossary Terms</i></b></p>      <dl>         <dd><a href="../glossary.html#attribute">attribute</a>, <a href="../glossary.html#declaration">declaration</a>,           <a href="../glossary.html#element"> </a><a href="../glossary.html#DTD">DTD</a>,           <a href="../glossary.html#element">element</a>, <a href="../glossary.html#namespace">namespace</a>,           <a href="../glossary.html#tag">tag</a>, <a href="../glossary.html#XHTML">XHTML</a></dd>      </dl>    </td>  </tr></table><p>Let's start out by writing up a simple version of 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. <h3><a name="create"></a>Creating the File</h3><p>Using a standard text editor, create a file called <code>slideSample.xml</code>.</p><blockquote>   <p><b>Note: </b>Here is a version of it that already exists: <a href="samples/slideSample01.xml"><code>slideSample01.xml</code></a>.     You can use this version to compare your work, or just review it as you read     this guide.</p></blockquote><p></p><h3><a name="declaration"></a>Writing the Declaration</h3><p>Next, write the <a href="../glossary.html#declaration">declaration</a>, which   identifies the file as an XML document. The declaration starts with the characters   &quot;<code>&lt;?</code>&quot;, which is the standard XML identifier for a <i>processor   instruction</i>. (You'll see other processor instructions later on in this tutorial.)</p><blockquote>   <pre>&lt;?xml version='1.0' encoding='us-ascii'?> </pre></blockquote><h4></h4>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 US ASCII character-encoding scheme. Since it has not been specified as a &quot;standalone&quot; document, the parser assumes that it may contain references to other documents. (To see how to specify a document as &quot;standalone&quot;, see <a href="../overview/1_xml.html#prolog">A Quick Introduction to XML, The XML Prolog</a>.) <h3><a name="comment"></a>Adding a Comment</h3><p>Comments are ignored by XML parsers. You never see them in fact, unless you   activate special settings in the parser. You'll see how to do that later on   in the tutorial, when we discuss <a href="8_lex.html">Using a LexicalEventListener</a>.   For now, add the text highlighted below to put a comment into the file.</p><blockquote>   <pre>&lt;?xml version='1.0' encoding='us-ascii'?> <b>&lt;!-- A SAMPLE set of slides --> </b></pre></blockquote><h4></h4><h3><a name="root"></a>Defining the Root Element</h3>After the declaration, every XML file defines exactly one <a href="../glossary.html#element">element</a>, known as the <i>root element</i>. Any other elements in the file are contained <i>within</i> that element. Enter the text highlighted below to define the root element for this file, <code>slideshow</code>: <blockquote>   <pre>&lt;?xml version='1.0' encoding='us-ascii'?> &lt;!-- A SAMPLE set of slides --> <b>&lt;slideshow> &lt;/slideshow></b></pre></blockquote><h3><a name="attributes"></a>Adding Attributes to an Element</h3><p>A slide presentation has a number of associated data items, none of which require   any structure. So it is natural to define them as <a href="../glossary.html#attribute">attributes</a>   of the <code>slideshow</code> element. Add the text highlighted below to set   up some attributes:</p><blockquote>   <pre>...&lt;slideshow <b>    title="Sample Slide Show"    date="Date of publication"    author="Yours Truly"</b>    >&lt;/slideshow></pre></blockquote><p>When you create a name for a <a href="../glossary.html#tag">tag</a> or an attribute,   you can use hyphens (&quot;-&quot;), underscores (&quot;_&quot;), colons (&quot;:&quot;),   and periods (&quot;.&quot;) in addition to characters and numbers.</p><blockquote>   <p><b>Note:</b><br>    Colons should be used with care or avoided altogether, because they are used     when defining the <a href="../glossary.html#namespace">namespace</a> for an     XML document.</p></blockquote><h3><a name="nesting"></a>Adding Nested Elements</h3><p>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><blockquote>   <pre>    ...<br>    &lt;!-- TITLE SLIDE -->    &lt;slide title="Title of Talk"/>    <b>&lt;!-- TITLE SLIDE -->    &lt;slide type="all">        &lt;title>Wake up to WonderWidgets!&lt;/title>    &lt;/slide></b>&lt;/slideshow></pre></blockquote><p>Here you have also added a <i>type</i> 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>type=&quot;tech&quot;</code> or <code>type=&quot;exec&quot;</code>,   or identified as suitable for both with <code>type=&quot;all&quot;</code>. </p><p><a name="design"></a>More importantly, though, this example illustrates the   difference between things that are more usefully defined as elements (the <i>title</i>   element) and things that are more suitable as attributes (the <i>type</i> 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 <i>container</i> (is it tall or short, wide or narrow).   The title is a characteristic of the <i>contents</i> (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><h3><a name="html"></a>Adding HTML-Style Text</h3><p>Since XML lets you define any tags you want, it makes sense to define a set   of tags that look like HTML. The <a href="../glossary.html#XHTML">XHTML</a>   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>&lt;em&gt;</code>   tag for emphasis (usually rendered as italicized text): </p><blockquote>   <pre>     ...<br>    &lt;!-- TITLE SLIDE -->    &lt;slide type="all">        &lt;title>Wake up to WonderWidgets!&lt;/title>    &lt;/slide>    <b>&lt;!-- OVERVIEW -->    &lt;slide type="all">        &lt;title>Overview&lt;/title&gt;        &lt;item&gt;Why &lt;em&gt;WonderWidgets&lt;/em&gt; are great&lt;/item&gt;        &lt;item&gt;Who &lt;em&gt;buys&lt;/em&gt; WonderWidgets&lt;/item&gt;    &lt;/slide&gt;</b>    &lt;/slideshow></pre></blockquote><p> We'll see later that defining a <i>title</i> element conflicts with the XHTML   element that uses the same name. We'll discuss the mechanism that produces the   conflict (the <a href="../glossary.html#DTD">DTD</a>) and several possible solutions   when we cover <a href="7b_pe.html">Parsing the Parameterized DTD</a>.</p><h3><a name="empty"></a>Adding an Empty Element</h3><p>One major difference between HTML and XML, though, is that all XML must be   well formed -- 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><blockquote>   <pre>     ...<br>    &lt;!-- OVERVIEW -->    &lt;slide type="all">        &lt;title>Overview&lt;/title&gt;        &lt;item&gt;Why &lt;em&gt;WonderWidgets&lt;/em&gt; are great&lt;/item&gt;        <b>&lt;item/&gt;</b>        &lt;item&gt;Who &lt;em&gt;buys&lt;/em&gt; WonderWidgets&lt;/item&gt;    &lt;/slide&gt;    &lt;/slideshow></pre></blockquote>Note that any element can be empty element. All it takes is ending the tag with &quot;<code>/&gt;</code>&quot; instead of &quot;<code>&gt;</code>&quot;. You could do the same thing by entering <code>&lt;item&gt;&lt;/item&gt;</code>, which is equivalent. <h3><a name="finish"></a>The Finished Product</h3><p>Here is the completed version of the XML file:</p><blockquote>   <pre>&lt;?xml version='1.0' encoding='us-ascii'?>&lt;!--  A SAMPLE set of slides  -->&lt;slideshow     title="Sample Slide Show"    date="Date of publication"    author="Yours Truly"    >    &lt;!-- TITLE SLIDE -->    &lt;slide type="all">      &lt;title>Wake up to WonderWidgets!&lt;/title><br>    &lt;/slide&gt;<br>    &lt;!-- OVERVIEW -->    &lt;slide type="all">        &lt;title>Overview&lt;/title&gt;        &lt;item&gt;Why &lt;em&gt;WonderWidgets&lt;/em&gt; are great&lt;/item&gt;        &lt;item/&gt;        &lt;item&gt;Who &lt;em&gt;buys&lt;/em&gt; WonderWidgets&lt;/item&gt;    &lt;/slide&gt;<br>&lt;/slideshow></pre></blockquote><p>Now that you've created a file to work with, you're ready to write a program   to echo it using the SAX parser. You'll do that in the next section.</p><blockquote>  <hr size=4></blockquote><p><p> <table width="100%"><tr>    <td align=left> <a href="index.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><ahref="2a_echo.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=bottom border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=bottom border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=bottom border=0 alt="Top | "></a>     </td><td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a>       <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a></td></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -