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

📄 2b_echo.html

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><!--NewPage--><html><head><title>Echoing an XML File with the SAX Parser</title><style type="text/css"><!----></style></head><body BGCOLOR="#ffffff"><table width="100%"><tr>    <td align=left> <a href="2a_echo.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><ahref="3_error.html"><img src="../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top 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>2b. Adding Additional Event Handlers</h2><table width="40%" border="1" align="right">  <tr>     <td>       <div align="center"><b><i>Link Summary</i></b></div>    </td>  </tr>  <tr>     <td>       <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>      </ul>      <dl>        <dt><b><i>Examples</i></b></dt>      </dl>      <ul>        <li><a href="work/Echo04.java">Echo04.java</a></li>        <li><a href="work/Echo04-01.log">Echo04-01.log</a></li>        <li><a href="samples/slideSample02.xml">slideSample02.xml</a></li>        <li><a href="work/Echo05.java">Echo05.java</a></li>        <li><a href="work/Echo05-02.log">Echo05-02.log</a></li>      </ul>      <p><b><i>API References</i></b></p>      <ul>        <li><a href="../../api/org/xml/sax/Locator.html">Locator</a></li>      </ul>      <p><b><i>Glossary Terms</i></b></p>      <dl>         <dd><a href="../glossary.html#URL">URL</a>, <a href="../glossary.html#URN">URN</a>         </dd>      </dl>    </td>  </tr></table><p>Besides <code>ignorableWhitespace</code>, there are only two other methods   in the <code>DocumentHandler</code> interface: <code>setDocumentLocator</code>   and <code>processingInstruction</code>. In this section of the tutorial, you'll   implement those two event handlers. <h3><a name="location"></a>Identifying the Document's Location</h3><p>A <i>locator</i> is an object that contains the information necessary to find   the document. The <a href="../../api/org/xml/sax/Locator.html"><code>Locator</code></a>   class encapsulates a system ID (<a href="../glossary.html#URL">URL</a>) or a   public identifier (<a href="../glossary.html#URN">URN</a>), or both. You would   need that information if you wanted to find something relative to the current   document -- in the same way, for example, that an HTML browser processes an   <code>href=&quot;anotherFile&quot;</code> attribute in an anchor tag -- the   browser uses the location of the current document to find <code>anotherFile</code>. </p><p>You could also use the locator to print out good diagnostic messages. In addition   to the document's location and public identifier, the locator contains methods   that give the column and line number of the most recently-processed event. The   <code>setDocumentLocator</code> method is called only once at the beginning   of the parse, though. To get the current line or column number, you would save   the locator when <code>setDocumentLocator</code> is invoked and then use it   in the other event-handling methods.</p><blockquote>   <p><b>Note:</b> <br>    The code discussed in this section is in <a href="work/Echo04.java"><code>Echo04.java</code></a>.     Its output is stored at <a href="work/Echo04-01.log"><code>Echo04-01.log</code></a>.</p></blockquote><p>Add the method below to the Echo program to get the document locator and use   it to echo the document's system ID.</p><h4></h4><blockquote>   <pre>    ...    private String indentString = "    "; // Amount to indent    private int indentLevel = 0;  <new><b>    public void setDocumentLocator (Locator l)    {        try {          out.write ("LOCATOR");          out.write ("\n SYS ID: " + l.getSystemId() );          out.flush ();        } catch (IOException e) {            // Ignore errors        }    }</b></new>    public void startDocument ()    ...</pre></blockquote><p><b>Notes:</b></p><ul>  <li>     <p>This method, in contrast to every other <code>DocumentHandler</code> method,       does not return a <code>SAXException</code>. So, rather than using <code>emit</code>       for output, this code writes directly to <code>System.out</code>. (This       method is generally expected to simply save the <code>Locator</code> for       later use, rather than do the kind of processing that generates an exception,       as here.)</p>  </li>  <li>The spelling of these methods is &quot;Id&quot;, not &quot;ID&quot;. So     you have <code>getSystemId</code> and <code>getPublicId</code>.</li></ul><p>When you compile and run the program on <code>slideSample01.xml</code>, here   is the significant part of the output:</p><blockquote>   <pre><b>LOCATOR SYS ID: file:<i>&lt;path&gt;</i>/../samples/slideSample01.xml</b>START DOCUMENT&lt;?xml version='1.0' encoding='UTF-8'?>...</pre></blockquote><a name="eventOrder"></a>Here, it is apparent that <code>setDocumentLocator</code> is called before <code>startDocument</code>. That can make a difference if you do any initialization in the event handling code. <h3><a name="processingInstructions"></a>Handling Processing Instructions</h3><p>It sometimes makes sense to code application-specific processing instructions   in the XML data. In this exercise, you'll add a processing instruction to your   <code>slideSample.xml</code> file and then modify the Echo program to display   it.</p><blockquote>   <p><b>Note:</b> <br>    The code discussed in this section is in <a href="work/Echo05.java"><code>Echo05.java</code></a>.     The file it operates on is <a href="samples/slideSample02.xml"><code>slideSample02.xml</code></a>.     The output is stored at <a href="work/Echo05-02.log"><code>Echo05-02.log</code></a>.   </p></blockquote><p>As you saw in <a href="../overview/1_xml.html">A Quick Introduction to XML</a>,   the format for a processing instruction is <code>&lt;?target data?&gt;</code>,   where &quot;target&quot; is the target application that is expected to do the   processing, and &quot;data&quot; is the instruction or information for it to   process. Add the text highlighted below to add a processing instruction for   a mythical slide presentation program that will query the user to find out which   slides to display (technical, executive-level, or all):</p><blockquote>   <pre>&lt;slideshow     ...    >    <new><b>    &lt;!-- PROCESSING INSTRUCTION -->    &lt;?my.presentation.Program: QUERY="exec, tech, all"?></b></new>    &lt;!-- TITLE SLIDE --></pre></blockquote><p><b>Notes:</b></p><ul>  <li>     <p>The &quot;data&quot; portion of the processing instruction can contain       spaces, or may even be null. But there cannot be any space between the initial       <code>&lt;?</code> and the target identifier.</p>  </li>  <li>     <p>The data begins after the first space.</p>  </li>  <li>     <p>Fully qualifying the target with the complete web-unique package prefix       makes sense, so as to preclude any conflict with other programs that might       process the same data.</p>  </li>  <li>In this case, the target includes a colon (:) after the name of the application.     That is probably atypical, but it seemed like a good idea for readability.   </li></ul><p>Now that you have a processing instruction to work with, add the code highlighted   below to the Echo app:</p><blockquote>   <pre>public void characters (char buf [], int offset, int len)...}    <new><b>public void processingInstruction (String target, String data)throws SAXException{    nl();     emit ("PROCESS: ");    emit ("&lt;?"+target+" "+data+"?>");}</b></new>private void emit (String s)...</pre></blockquote><p>When your edits are complete, compile and run the program. The relevant part   of the output should look like this:</p><blockquote>   <pre>...CHARS:   CHARS:   <b>PROCESS: &lt;?my.presentation.Program: QUERY="exec, tech, all"?></b>CHARS:   CHARS:   ...</pre></blockquote>Now that you've had a chance to work with the processing instruction, you can remove that instruction from the XML file. You won't be needing it any more. <h3><a name="summary"></a>Summary</h3><p>With the minor exception of <code>ignorableWhitespace</code>, you have used   all of the methods in the <code>DocumentHandler</code> interface to handle SAX   events. You'll see <code>ignorableWhitespace</code> a little later on. Next,   though, you'll get deeper insight into how you handle errors in the SAX parsing   process.</p><blockquote></blockquote><blockquote><hr size=4></blockquote><p><p> <table width="100%"><tr>    <td align=left> <a href="2a_echo.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><ahref="3_error.html"><img src="../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top 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 + -