📄 jaxpsax4.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>Adding Additional Event Handlers</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="JAXPSAX3.html" /> <link rel="Next" href="JAXPSAX5.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="JAXPSAX3.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="JAXPSAX5.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="wp64500"> </a><h2 class="pHeading1">Adding Additional Event Handlers</h2><a name="wp64501"> </a><p class="pBody">Besides <code class="cCode">ignorableWhitespace</code>, there are two other <code class="cCode">ContentHandler</code> methods that can find uses in even simple applications: <code class="cCode">setDocumentLocator</code> and <code class="cCode">processingInstruction</code>. In this section of the tutorial, you'll implement those two event handlers. </p><a name="wp64503"> </a><h3 class="pHeading2">Identifying the Document's Location</h3><a name="wp64504"> </a><p class="pBody">A <span style="font-style: italic">locator</span> is an object that contains the information necessary to find the document. The <code class="cCode">Locator</code> class encapsulates a system ID (URL) or a public identifier (URN), 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 class="cCode">href="anotherFile"</code> attribute in an anchor tag--the browser uses the location of the current document to find <code class="cCode">anotherFile</code>. </p><a name="wp64505"> </a><p class="pBody">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 class="cCode">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 class="cCode">setDocumentLocator</code> is invoked and then use it in the other event-handling methods.</p><hr><a name="wp67827"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo04.java" target="_blank">Echo04.java</a></code>. Its output is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo04-01.txt" target="_blank">Echo04-01.txt</a></code>. (The browsable version is <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo04-01.html" target="_blank">Echo04-01.html</a></code>.)</p><hr><a name="wp67828"> </a><p class="pBody">Start by removing the extra character-echoing code you added for the last example:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void characters(char buf[], int offset, int len)throws SAXException{ <code class="cCodeStruck">if (textBuffer != null) { echoText(); textBuffer = null; }</code> String s = new String(buf, offset, len); ...}<a name="wp71879"> </a></pre></div><a name="wp71862"> </a><p class="pBody">Next. add the method highlighted below to the Echo program to get the document locator and use it to echo the document's system ID.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...private String indentString = " "; // Amount to indentprivate int indentLevel = 0;<a name="wp64508"> </a><code class="cCodeBold">public void setDocumentLocator(Locator l){ try { out.write("LOCATOR"); out.write("SYS ID: " + l.getSystemId() ); out.flush(); } catch (IOException e) { // Ignore errors }}</code><a name="wp64509"> </a>public void startDocument()...<a name="wp64510"> </a></pre></div><a name="wp64511"> </a><p class="pBody">Notes:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64512"> </a><div class="pSmartList1"><li>This method, in contrast to every other <code class="cCode">ContentHandler</code> method, does not return a <code class="cCode">SAXException</code>. So, rather than using <code class="cCode">emit</code> for output, this code writes directly to <code class="cCode">System.out</code>. (This method is generally expected to simply save the <code class="cCode">Locator</code> for later use, rather than do the kind of processing that generates an exception, as here.)</li></div><a name="wp64513"> </a><div class="pSmartList1"><li>The spelling of these methods is "<code class="cCode">Id"</code>, not "<code class="cCode">ID"</code>. So you have <code class="cCode">getSystemId</code> and <code class="cCode">getPublicId</code>.</li></div></ul></div><a name="wp64514"> </a><p class="pBody">When you compile and run the program on <code class="cCode">slideSample01.xml</code>, here is the significant part of the output:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">LOCATORSYS ID: file:<code class="cVariable"><path></code>/../samples/slideSample01.xml<a name="wp64515"> </a>START DOCUMENT<?xml version='1.0' encoding='UTF-8'?>...<a name="wp64516"> </a></pre></div><a name="wp64518"> </a><p class="pBody">Here, it is apparent that <code class="cCode">setDocumentLocator</code> is called before startDocument. That can make a difference if you do any initialization in the event handling code. </p><a name="wp64520"> </a><h3 class="pHeading2">Handling Processing Instructions</h3><a name="wp64521"> </a><p class="pBody">It sometimes makes sense to code application-specific processing instructions in the XML data. In this exercise, you'll modify the <code class="cCode">Echo</code> program to display a processing instruction contained in <code class="cCode">slideSample02.xml</code>.</p><hr><a name="wp64522"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo05.java" target="_blank">Echo05.java</a></code>. The file it operates on is <code class="cCode"><a href="../examples/xml/samples/slideSample02.xml" target="_blank">slideSample02.xml</a></code>, as described in <a href="IntroXML4.html#wp67861">Writing Processing Instructions </a>. The output is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo05-02.txt" target="_blank">Echo05-02.txt</a></code>. (The browsable versions are <code class="cCode"><a href="../examples/xml/samples/slideSample02-xml.html" target="_blank">slideSample02-xml.html</a></code> and<code class="cCode"><a href="../examples/jaxp/sax/samples/Echo05-02.html" target="_blank"> Echo05-02.html</a></code>.) </p><hr><a name="wp64526"> </a><p class="pBody">As you saw in <a href="IntroXML4.html#wp67861">Writing Processing Instructions</a>, the format for a processing instruction is <code class="cCode"><?target data?></code>, where "target" is the target application that is expected to do the processing, and "data" is the instruction or information for it to process. The sample file <code class="cCode">slideSample02.xml</code> contains a processing instruction for a mythical slide presentation program that queries the user to find out which slides to display (technical, executive-level, or all):</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><slideshow ... > <code class="cCodeBold"><!-- PROCESSING INSTRUCTION --> <?my.presentation.Program QUERY="exec, tech, all"?></code> <!-- TITLE SLIDE --><a name="wp64527"> </a></pre></div><a name="wp64537"> </a><p class="pBody">To display that processing instruction, add the code highlighted below to the Echo app:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void characters(char buf[], int offset, int len)...}<a name="wp64538"> </a><code class="cCodeBold">public void processingInstruction(String target, String data)throws SAXException{ nl(); emit("PROCESS: "); emit("<?"+target+" "+data+"?>");}</code><a name="wp64539"> </a>private void echoText()...<a name="wp64540"> </a></pre></div><a name="wp64541"> </a><p class="pBody">When your edits are complete, compile and run the program. The relevant part of the output should look like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ELEMENT: <slideshow ...>PROCESS: <?my.presentation.Program QUERY="exec, tech, all"?>CHARS: ...<a name="wp64542"> </a></pre></div><a name="wp64545"> </a><h3 class="pHeading2">Summary</h3><a name="wp64546"> </a><p class="pBody">With the minor exception of <code class="cCode">ignorableWhitespace</code>, you have used most of the <code class="cCode">ContentHandler</code> methods that you need to handle the most commonly useful SAX events. You'll see <code class="cCode">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> <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="JAXPSAX3.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="JAXPSAX5.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 + -