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

📄 jaxpsax6.html

📁 j2eePDF格式的电子书
💻 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>Displaying Special Characters and CDATA</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="JAXPSAX5.html" />    <link rel="Next" href="JAXPSAX7.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="JAXPSAX5.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="JAXPSAX7.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="wp64737"> </a><h2 class="pHeading1">Displaying Special Characters and CDATA</h2><a name="wp64738"> </a><p class="pBody">The next thing we want to do with the parser is to customize it a bit, so you can see how to get information it usually ignores. In this section, you'll learn how the parser handles: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64739"> </a><div class="pSmartList1"><li>&nbsp;&nbsp;Special Characters (<code class="cCode">&quot;&lt;&quot;</code>, <code class="cCode">&quot;&amp;&quot;</code>, and so on)</li></div><a name="wp64740"> </a><div class="pSmartList1"><li>&nbsp;&nbsp;Text with XML-style syntax</li></div></ul></div><a name="wp64742"> </a><h3 class="pHeading2">Handling Special Characters</h3><a name="wp64743"> </a><p class="pBody">In XML, an entity is an XML structure (or plain text) that has a name. Referencing the entity by name causes it to be inserted into the document in place of the entity reference. To create an entity reference, the entity name is surrounded by an ampersand and a semicolon, like this: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">  &amp;entityName;<a name="wp64744"> </a></pre></div><a name="wp67017"> </a><p class="pBody">Earlier, you put an entity reference into your XML document by coding:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Market Size &amp;lt; predicted<a name="wp94847"> </a></pre></div><hr><a name="wp68113"> </a><p class="pNote">Note: The file containing this XML is <code class="cCode"><a  href="../examples/xml/samples/slideSample03.xml" target="_blank">slideSample03.xml</a></code>, as described in <a  href="IntroXML4.html#wp67725">Using an Entity Reference in an XML Document</a>. The results of processing it are shown in <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo07-03.txt" target="_blank">Echo07-03.txt</a></code>. (The browsable versions are <code class="cCode"><a  href="../examples/xml/samples/slideSample03-xml.html" target="_blank">slideSample03-xml.html</a></code> and <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo07-03.html" target="_blank">Echo07-03.html</a></code>.) </p><hr><a name="wp64796"> </a><p class="pBody">When you run the Echo program on <code class="cCode">slideSample03.xml</code>, you see the following output:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ELEMENT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&gt;CHARS:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Market Size &lt; predictedEND_ELM:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/item&gt;<a name="wp64797"> </a></pre></div><a name="wp64798"> </a><p class="pBody">The parser converted the reference into the entity it represents, and passed the entity to the application.</p><a name="wp64800"> </a><h3 class="pHeading2">Handling Text with XML-Style Syntax</h3><a name="wp64801"> </a><p class="pBody">When you are handling large blocks of XML or HTML that include many of the special characters, you use a <code class="cCode">CDATA</code> section.</p><hr><a name="wp64802"> </a><p class="pNote">Note: The XML file used in this example is <code class="cCode"><a  href="../examples/xml/samples/slideSample04.xml" target="_blank">slideSample04.xml</a></code>, as described in <a  href="JAXPSAX6.html#wp64800">Handling Text with XML-Style Syntax</a>. The results of processing it are shown in <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo07-04.txt" target="_blank">Echo07-04.txt</a></code>. (The browsable versions are <code class="cCode"><a  href="../examples/xml/samples/slideSample04-xml.html" target="_blank">slideSample04-xml.html</a></code> and <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo07-04.html" target="_blank">Echo07-04.html</a></code>.)</p><hr><a name="wp64803"> </a><p class="pBody">A <code class="cCode">CDATA</code> section works like <code class="cCode">&lt;pre&gt;...&lt;/pre&gt;</code> in HTML, only more so--all whitespace in a <code class="cCode">CDATA</code> section is significant, and characters in it are not interpreted as XML. A <code class="cCode">CDATA</code> section starts with <code class="cCode">&lt;![CDATA[ </code>and ends with <code class="cCode">]]&gt;</code>. The file <code class="cCode">slideSample04.xml</code> contains this a <code class="cCode">CDATA</code> section for a fictitious technical slide:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp; ...<code class="cCodeBold">&nbsp;&nbsp;&lt;slide type=&quot;tech&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;How it Works&lt;/title&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&gt;First we fozzle the frobmorten&lt;/item&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&gt;Then we framboze the staten&lt;/item&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&gt;Finally, we frenzle the fuznaten&lt;/item&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;item&gt;&lt;![CDATA[Diagram:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frobmorten &lt;--------------- fuznaten&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;3&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;&nbsp;&lt;1&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&lt;1&gt; = fozzle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; V &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&lt;2&gt; = framboze &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Staten-------------------------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;3&gt; = frenzle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;2&gt;&nbsp;&nbsp;&nbsp;&nbsp;]]&gt;&lt;/item&gt;&nbsp;&nbsp;&lt;/slide&gt;</code>&lt;/slideshow&gt;<a name="wp95906"> </a></pre></div><a name="wp64806"> </a><p class="pBody">When you run the Echo program on the new file, you see the following output:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;ELEMENT: &lt;item&gt;&nbsp;&nbsp;CHARS:   Diagram:frobmorten &lt;--------------fuznaten&nbsp;&nbsp;&nbsp;&nbsp;| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;3&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ^&nbsp;&nbsp;&nbsp; | &lt;1&gt;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|   &lt;1&gt; = fozzle&nbsp;&nbsp;&nbsp;&nbsp;V  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|   &lt;2&gt; = framboze &nbsp;&nbsp;staten---------------------+   &lt;3&gt; = frenzle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;2&gt;END_ELM: &lt;/item&gt;<a name="wp72289"> </a></pre></div><a name="wp68850"> </a><p class="pBody">You can see here that the text in the <code class="cCode">CDATA</code> section arrived as it was written. Since the parser didn't treat the angle brackets as XML, they didn't generate the fatal errors they would otherwise cause. (Because, if the angle brackets weren't in a CDATA section, the document would not be well-formed.)</p><a name="wp64811"> </a><h3 class="pHeading2">Handling CDATA and Other Characters</h3><a name="wp64812"> </a><p class="pBody">The existence of CDATA makes the proper echoing of XML a bit tricky. If the text to be output is <span style="font-style: italic">not</span> in a CDATA section, then any angle brackets, ampersands, and other special characters in the text should be replaced with the appropriate entity reference. (Replacing left angle brackets and ampersands is most important, other characters will be interpreted properly without misleading the parser.) </p><a name="wp64813"> </a><p class="pBody">But if the output text <span style="font-style: italic">is</span> in a CDATA section, then the substitutions should not occur, to produce text like that in the example above. In a simple program like our Echo application, it's not a big deal. But many XML-filtering applications will want to keep track of whether the text appears in a CDATA section, in order to treat special characters properly. (Later in this tutorial, you will see how to use a <code class="cCode">LexicalHandler</code> to find out whether or not you are processing a CDATA section.)</p><a name="wp64814"> </a><p class="pBody">One other area to watch for is attributes. The text of an attribute value could also contain angle brackets and semicolons that need to be replaced by entity references. (Attribute text can never be in a CDATA section, though, so there is never any question about doing that substitution.) </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="JAXPSAX5.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="JAXPSAX7.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 + -