📄 jaxpsax11.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>Handling Lexical Events</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="JAXPSAX10.html" /> <link rel="Next" href="JAXPSAX12.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="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"> <blockquote><a name="wp65510"> </a><h2 class="pHeading1">Handling Lexical Events</h2><a name="wp65511"> </a><p class="pBody">You saw earlier that if you are writing text out as XML, you need to know if you are in a <code class="cCode">CDATA</code> section. If you are, then angle brackets (<) and ampersands (&) should be output unchanged. But if you're not in a <code class="cCode">CDATA</code> section, they should be replaced by the predefined entities <code class="cCode">&lt;</code> and <code class="cCode">&amp;</code>. But how do you know if you're processing a <code class="cCode">CDATA</code> section? </p><a name="wp65512"> </a><p class="pBody">Then again, if you are filtering XML in some way, you would want to pass comments along. Normally the parser ignores comments. How can you get comments so that you can echo them? </p><a name="wp65513"> </a><p class="pBody">Finally, there are the parsed entity definitions. If an XML-filtering app sees <code class="cCode">&myEntity;</code> it needs to echo the same string--not the text that is inserted in its place. How do you go about doing that? </p><a name="wp65514"> </a><p class="pBody">This section of the tutorial answers those questions. It shows you how to use <code class="cCode">org.xml.sax.ext.LexicalHandler</code> to identify comments, <code class="cCode">CDATA</code> sections, and references to parsed entities. </p><a name="wp65515"> </a><p class="pBody">Comments, <code class="cCode">CDATA</code> tags, and references to parsed entities constitute <span style="font-style: italic">lexical</span> information--that is, information that concerns the text of the XML itself, rather than the XML's information content. Most applications, of course, are concerned only with the <span style="font-style: italic">content</span> of an XML document. Such apps will not use the <code class="cCode">LexicalEventListener</code> API. But apps that output XML text will find it invaluable. </p><hr><a name="wp65516"> </a><p class="pNote">Note: Lexical event handling is a optional parser feature. Parser implementations are not required to support it. (The reference implementation does so.) This discussion assumes that the parser you are using does so, as well.</p><hr><a name="wp65518"> </a><h3 class="pHeading2">How the LexicalHandler Works</h3><a name="wp65519"> </a><p class="pBody">To be informed when the SAX parser sees lexical information, you configure the <code class="cCode">XmlReader</code> that underlies the parser with a <code class="cCode">LexicalHandler</code>. The <code class="cCode">LexicalHandler</code> interface defines these even-handling methods:</p><a name="wp65523"> </a><p class="pDefinitionTerm"><code class="cCode">comment(String comment)</code></p><a name="wp65524"> </a><p class="pDefinition">Passes comments to the application.</p><a name="wp65525"> </a><p class="pDefinitionTerm"><code class="cCode">startCDATA(), endCDATA()</code></p><a name="wp65526"> </a><p class="pDefinition">Tells when a <code class="cCode">CDATA</code> section is starting and ending, which tells your application what kind of characters to expect the next time <code class="cCode">characters()</code> is called.</p><a name="wp65527"> </a><p class="pDefinitionTerm"><code class="cCode">startEntity(String name), endEntity(String name)</code></p><a name="wp65528"> </a><p class="pDefinition">Gives the name of a parsed entity.</p><a name="wp65529"> </a><p class="pDefinitionTerm"><code class="cCode">startDTD(String name, String publicId, String systemId), endDTD()</code></p><a name="wp65530"> </a><p class="pDefinition">Tells when a DTD is being processed, and identifies it.</p><a name="wp65532"> </a><h3 class="pHeading2">Working with a LexicalHandler</h3><a name="wp69127"> </a><p class="pBody">In the remainder of this section, you'll convert the Echo app into a lexical handler and play with its features. </p><hr><a name="wp69128"> </a><p class="pNote">Note: The code shown in this section is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo11.java" target="_blank">Echo11.java</a></code>. The output is shown in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo11-09.txt" target="_blank">Echo11-09.txt</a></code>. (The browsable version is <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo11-09.html" target="_blank">Echo11-09.html</a></code>.)</p><hr><a name="wp69129"> </a><p class="pBody">To start, add the code highlighted below to implement the <code class="cCode">LexicalHandler</code> interface and add the appropriate methods.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><span style="font-weight: normal">import org.xml.sax.*;import org.xml.sax.helpers.DefaultHandler;</span><code class="cCodeBold">import org.xml.sax.ext.LexicalHandler;...</code>public class Echo extends HandlerBase <code class="cCodeBold">implements LexicalHandler</code>{ public static void main(String argv[]) { ... // Use an instance of ourselves as the SAX event handler <code class="cCodeStruck">DefaultHandler handler = new Echo();</code> <code class="cCodeBold">Echo handler = new Echo();</code> ...<a name="wp69036"> </a></pre></div><a name="wp65537"> </a><p class="pBody">At this point, the <code class="cCode">Echo</code> class extends one class and implements an additional interface. You changed the class of the handler variable accordingly, so you can use the same instance as either a <code class="cCode">DefaultHandler</code> or a <code class="cCode">LexicalHandler</code>, as appropriate. </p><a name="wp65538"> </a><p class="pBody">Next, add the code highlighted below to get the <code class="cCode">XMLReader</code> that the parser delegates to, and configure it to send lexical events to your lexical handler:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void main(String argv[]){ ... try { ... // Parse the input SAXParser saxParser = factory.newSAXParser();<code class="cCodeBold"> XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setProperty( "http://xml.org/sax/properties/lexical-handler", handler ); </code> saxParser.parse( new File(argv[0]), handler); } catch (SAXParseException spe) { ...<a name="wp65539"> </a></pre></div><a name="wp65540"> </a><p class="pBody">Here, you configured the <code class="cCode">XMLReader</code> using the <code class="cCode">setProperty()</code> method defined in the <code class="cCode">XMLReader</code> class. The property name, defined as part of the SAX standard, is the URL, <code class="cCode">http://xml.org/sax/properties/lexical-handler</code>.</p><a name="wp92567"> </a><p class="pBody">Finally, add the code highlighted below to define the appropriate methods that implement the interface.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void warning(SAXParseException err) ...}<code class="cCodeBold">public void comment(char[] ch, int start, int length)throws SAX-Exception{}public void startCDATA()throws SAXException{}pubic void endCDATA()throws SAXException{}public void startEntity(String name)throws SAXException{}public void endEntity(String name)throws SAXException{}public void startDTD( String name, String publicId, String systemId)throws SAXException{}public void endDTD()throws SAXException{}</code>private void echoText()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -