📄 jaxpsax11.html
字号:
...<a name="wp65542"> </a></pre></div><a name="wp65551"> </a><p class="pBody">You have now turned the <code class="cCode">Echo</code> class into a lexical handler. In the next section, you'll start experimenting with lexical events.</p><a name="wp65553"> </a><h4 class="pHeading3">Echoing Comments</h4><a name="wp65554"> </a><p class="pBody">The next step is to do something with one of the new methods. Add the code highlighted below to echo comments in the XML file:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void comment(char[] ch, int start, int length) throws SAXException{<code class="cCodeBold"> String text = new String(ch, start, length); nl(); emit("COMMENT: "+text);</code>}<a name="wp65555"> </a></pre></div><a name="wp65556"> </a><p class="pBody">When you compile the Echo program and run it on your XML file, the result looks something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">COMMENT: A SAMPLE set of slides COMMENT: FOR WALLY / WALLIES COMMENT: DTD for a simple "slide show".<a name="wp65557"> </a>COMMENT: Defines the %inline; declaration COMMENT: ...<a name="wp65558"> </a></pre></div><a name="wp65560"> </a><p class="pBody">The line endings in the comments are passed as part of the comment string, once again normalized to newlines. You can also see that comments in the DTD are echoed along with comments from the file. (That can pose problems when you want to echo only comments that are in the data file. To get around that problem, you can use the <code class="cCode">startDTD</code> and <code class="cCode">endDTD</code> methods.)</p><a name="wp65562"> </a><h4 class="pHeading3">Echoing Other Lexical Information</h4><a name="wp65563"> </a><p class="pBody">To finish up this section, you'll exercise the remaining <code class="cCode">LexicalHandler</code> methods. </p><hr><a name="wp65564"> </a><p class="pNote">Note: The code shown in this section is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo12.java" target="_blank">Echo12.java</a></code>. The file it operates on is <code class="cCode"><a href="../examples/xml/samples/slideSample09.xml" target="_blank">slideSample09.xml</a></code>. The results of processing are in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo12-09.txt" target="_blank">Echo12-09.txt</a></code> (The browsable versions are <code class="cCode"><a href="../examples/xml/samples/slideSample09-xml.html" target="_blank">slideSample09-xml.html</a></code> and <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo12-09.html" target="_blank">Echo12-09.html</a></code>.) </p><hr><a name="wp65565"> </a><p class="pBody">Make the changes highlighted below to remove the comment echo (you don't need that any more) and echo the other events, along with any characters that have been accumulated when an event occurs:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void comment(char[] ch, int start, int length)throws SAXException{<code class="cCodeStruck"> String text = new String(ch, start, length); nl(); emit("COMMENT: "+text);</code>}<a name="wp65566"> </a>public void startCDATA()throws SAXException{<code class="cCodeBold"> echoText(); nl(); emit("START CDATA SECTION");</code>}<a name="wp65567"> </a>public void endCDATA()throws SAXException{<code class="cCodeBold"> echoText(); nl(); emit("END CDATA SECTION");</code>}<a name="wp65568"> </a>public void startEntity(String name)throws SAXException{<code class="cCodeBold"> echoText(); nl(); emit("START ENTITY: "+name);</code>}<a name="wp65569"> </a>public void endEntity(String name)throws SAXException{<code class="cCodeBold"> echoText(); nl(); emit("END ENTITY: "+name);</code>}<a name="wp65570"> </a>public void startDTD(String name, String publicId, String systemId)throws SAXException{ <code class="cCodeBold">nl(); emit("START DTD: "+name +" publicId=" + publicId +" systemId=" + systemId);</code> }<a name="wp65571"> </a>public void endDTD()throws SAXException{ <code class="cCodeBold">nl(); emit("END DTD");</code> }<a name="wp65572"> </a></pre></div><a name="wp65573"> </a><p class="pBody">Here is what you see when the <code class="cCode">DTD</code> is processed:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">START DTD: slideshow publicId=null systemId=slideshow3.dtdSTART ENTITY: ......END DTD<a name="wp65574"> </a></pre></div><hr><a name="wp65575"> </a><p class="pNote">Note: To see events that occur while the <code class="cCode">DTD</code> is being processed, use <code class="cCode">org.xml.sax.ext.DeclHandler</code>.</p><hr><a name="wp65576"> </a><p class="pBody">Here is some of the additional output you see when the internally defined <code class="cCode">products</code> entity is processed with the latest version of the program: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code style="font-weight: bold" class="cCodeBold">START ENTITY: products</code><span style="font-weight: normal">CHARS: WonderWidgets</span><code class="cCodeBold">END ENTITY: products</code><a name="wp70706"> </a></pre></div><a name="wp70707"> </a><p class="pBody">And here is the additional output you see as a result of processing the external copyright entity: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold"> START ENTITY: copyright</code> CHARS: This is the standard copyright message that our lawyersmake us put everywhere so we don't have to shell out amillion bucks every time someone spills hot coffee in theirlap...<code class="cCodeBold"> END ENTITY: copyright</code><a name="wp65580"> </a></pre></div><a name="wp69140"> </a><p class="pBody">Finally, you get output that shows when the <code class="cCode">CDATA</code> section was processed:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code style="font-weight: bold" class="cCodeBold"> START CDATA SECTION</code><span style="font-weight: normal"> CHARS: Diagram:frobmorten <--------------fuznaten | <3> ^ | <1> | <1> = fozzle V | <2> = framboze staten---------------------+ <3> = frenzle <2></span><code class="cCodeBold"> END CDATA SECTION</code><a name="wp69175"> </a></pre></div><a name="wp70661"> </a><p class="pBody">In summary, the <code class="cCode">LexicalHandler</code> gives you the event-notifications you need to produce an accurate reflection of the original XML text. </p><hr><a name="wp70727"> </a><p class="pNote">Note: To accurately echo the input, you would modify the characters() method to echo the text it sees in the appropriate fashion, depending on whether or not the program was in <code class="cCode">CDATA</code> mode.</p><hr> </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="JAXPSAX10.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="JAXPSAX12.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 + -