📄 1_write.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 "<code><?</code>", 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><?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 "standalone" document, the parser assumes that it may contain references to other documents. (To see how to specify a document as "standalone", 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><?xml version='1.0' encoding='us-ascii'?> <b><!-- 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><?xml version='1.0' encoding='us-ascii'?> <!-- A SAMPLE set of slides --> <b><slideshow> </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>...<slideshow <b> title="Sample Slide Show" date="Date of publication" author="Yours Truly"</b> ></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 ("-"), underscores ("_"), colons (":"), and periods (".") 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> <!-- TITLE SLIDE --> <slide title="Title of Talk"/> <b><!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide></b></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="tech"</code> or <code>type="exec"</code>, or identified as suitable for both with <code>type="all"</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><em></code> tag for emphasis (usually rendered as italicized text): </p><blockquote> <pre> ...<br> <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide> <b><!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <item>Who <em>buys</em> WonderWidgets</item> </slide></b> </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> <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <b><item/></b> <item>Who <em>buys</em> WonderWidgets</item> </slide> </slideshow></pre></blockquote>Note that any element can be empty element. All it takes is ending the tag with "<code>/></code>" instead of "<code>></code>". You could do the same thing by entering <code><item></item></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><?xml version='1.0' encoding='us-ascii'?><!-- A SAMPLE set of slides --><slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly" > <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title><br> </slide><br> <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <item/> <item>Who <em>buys</em> WonderWidgets</item> </slide><br></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 + -