📄 jaxpsax7.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>Parsing with a DTD</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="JAXPSAX6.html" /> <link rel="Next" href="JAXPSAX8.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="JAXPSAX6.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="JAXPSAX8.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="wp64852"> </a><h2 class="pHeading1">Parsing with a DTD</h2><a name="wp64853"> </a><p class="pBody">After the XML declaration, the document prolog can include a DTD, or reference an external DTD, or both. In this section, you'll see the effect of the DTD on the data that the parser delivers to your application.</p><a name="wp64977"> </a><h3 class="pHeading2">DTD's Effect on the Nonvalidating Parser</h3><a name="wp64978"> </a><p class="pBody">In this section, you'll use the Echo program to see how the data appears to the SAX parser when the data file references a DTD. </p><hr><a name="wp94962"> </a><p class="pNote">Note: The XML file used in this section is <code class="cCode"><a href="../examples/xml/samples/slideSample05.xml" target="_blank">slideSample05.xml</a></code>, which references <code class="cCode"><a href="../examples/xml/samples/slideshow1a.dtd" target="_blank">slideshow1a.dtd</a></code>, as described in <a href="JAXPSAX7.html#wp64852">Parsing with a DTD</a>. The output is shown in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo07-05.txt" target="_blank">Echo07-05.txt</a></code>. (The browsable versions are <code class="cCode"><a href="../examples/xml/samples/slideshow1a-dtd.html" target="_blank">slideshow1a-dtd.html</a></code>, <code class="cCode"><a href="../examples/xml/samples/slideSample05-xml.html" target="_blank">slideSample05-xml.html</a></code>, and <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo07-05.html" target="_blank">Echo07-05.html</a></code>.) </p><hr><a name="wp64980"> </a><p class="pBody">Running the Echo program on your latest version of <code class="cCode">slideSample.xml</code> shows that many of the superfluous calls to the <code class="cCode">characters</code> method have now disappeared.</p><a name="wp72039"> </a><p class="pBody">Where before you saw: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> ...>PROCESS: ...<code class="cCodeBold">CHARS:</code> ELEMENT: <slide ATTR: ... > ELEMENT: <title> CHARS: Wake up to ... END_ELM: </title> END_ELM: </slide><code class="cCodeBold">CHARS:</code> ELEMENT: <slide ATTR: ... > ...<a name="wp64981"> </a></pre></div><a name="wp64982"> </a><p class="pBody">Now you see:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> ...>PROCESS: ... ELEMENT: <slide ATTR: ... > ELEMENT: <title> CHARS: Wake up to ... END_ELM: </title> END_ELM: </slide> ELEMENT: <slide ATTR: ... > ...<a name="wp72041"> </a></pre></div><a name="wp72040"> </a><p class="pBody">It is evident here that the whitespace characters which were formerly being echoed around the <code class="cCode">slide</code> elements are no longer being delivered by the parser, because the DTD declares that <code class="cCode">slideshow</code> consists solely of <code class="cCode">slide</code> elements:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> <!ELEMENT slideshow (slide+)><a name="wp64983"> </a></pre></div><a name="wp64985"> </a><h3 class="pHeading2">Tracking Ignorable Whitespace</h3><a name="wp64986"> </a><p class="pBody">Now that the DTD is present, the parser is no longer calling the <code class="cCode">characters</code> method with whitespace that it knows to be irrelevant. From the standpoint of an application that is only interested in processing the XML data, that is great. The application is never bothered with whitespace that exists purely to make the XML file readable. </p><a name="wp64987"> </a><p class="pBody">On the other hand, if you were writing an application that was filtering an XML data file, and you wanted to output an equally readable version of the file, then that whitespace would no longer be irrelevant--it would be essential. To get those characters, you need to add the <code class="cCode">ignorableWhitespace</code> method to your application. You'll do that next.</p><hr><a name="wp64988"> </a><p class="pNote">Note: The code written in this section is contained in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo08.java" target="_blank">Echo08.java</a></code>. The output is in <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo08-05.txt" target="_blank">Echo08-05.txt</a></code>. (The browsable version is <code class="cCode"><a href="../examples/jaxp/sax/samples/Echo08-05.html" target="_blank">Echo08-05.html</a></code>.)</p><hr><a name="wp64989"> </a><p class="pBody">To process the (generally) ignorable whitespace that the parser is seeing, add the code highlighted below to implement the <code class="cCode">ignorableWhitespace</code> event handler in<span style="font-weight: bold"> </span>your version of the Echo program: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void characters (char buf[], int offset, int len)... }<a name="wp64990"> </a><code class="cCodeBold">public void ignorableWhitespace char buf[], int offset, int Len)throws SAXException{ nl(); emit("IGNORABLE");}</code><a name="wp64991"> </a>public void processingInstruction(String target, String data)...<a name="wp64992"> </a></pre></div><a name="wp64993"> </a><p class="pBody">This code simply generates a message to let you know that ignorable whitespace was seen.</p><hr><a name="wp64994"> </a><p class="pNote">Note: Again, not all parsers are created equal. The SAX specification does not require this method to be invoked. The Java XML implementation does so whenever the DTD makes it possible.</p><hr><a name="wp64995"> </a><p class="pBody">When you run the Echo application now, your output looks like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -