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

📄 ch04_02.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>Events and Handlers (Perl and XML)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Erik T. Ray and Jason McIntosh" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="059600205XL" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl and XML" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl &amp; XML" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">4.2. Events and Handlers</h2><p>Why<a name="INDEX-330" />do<a name="INDEX-331" /> we call it an<em class="emphasis">event</em> stream and not an element stream or amarkup object stream? The fact that XML is hierarchical (elementscontain other elements) makes it impossible to package individualelements and serve them up as tokens in the stream. In a well-formeddocument, all elements are contained in one root element. A rootelement that contains the whole document is not a stream. Thus, wereally can't expect a stream to give a completeelement in a token, unless it's an empty element.</p><p>Instead, XML streams are composed of events. An<em class="emphasis">event</em><a name="INDEX-332" />is a signal that the state of the document (as we'veseen it so far in the stream) has changed. For example, when theparser comes across the start tag for an element, it indicates thatanother element was opened and the state of parsing has changed. Anend tag affects the state by closing the most recently openedelement. An XML processor can keep track of open elements in a stackdata structure, pushing newly opened elements and popping off closedones. At any given moment during parsing, the processor knows howdeep it is in the document by the size of the stack.</p><p>Though parsers support a variety of events, there is a lot ofoverlap. For example, one parser may distinguish between a start tagand an empty element, while another may not, but all will signal thepresence of that element. Let's look more closely athow a parser might dole out tokens, as shown <a href="ch04_02.htm#perlxml-CHP-4-EX-1">Example 4-1</a>. </p><a name="perlxml-CHP-4-EX-1" /><div class="example"><h4 class="objtitle">Example 4-1. XML fragment </h4><blockquote><pre class="code">&lt;recipe&gt;  &lt;name&gt;peanut butter and jelly sandwich&lt;/name&gt;  &lt;!-- add picture of sandwich here --&gt;  &lt;ingredients&gt;    &lt;ingredient&gt;Gloppy&amp;trade; brand peanut butter&lt;/ingredient&gt;    &lt;ingredient&gt;bread&lt;/ingredient&gt;    &lt;ingredient&gt;jelly&lt;/ingredient&gt;  &lt;/ingredients&gt;  &lt;instructions&gt;    &lt;step&gt;Spread peanutbutter on one slice of bread.&lt;/step&gt;    &lt;step&gt;Spread jelly on the other slice of bread.&lt;/step&gt;    &lt;step&gt;Put bread slices together, with peanut butter and  jelly touching.&lt;/step&gt;  &lt;/instructions&gt;&lt;/recipe&gt;</pre></blockquote></div><p>Apply a parser to the preceding example and it might generate thislist of events:</p><ol><li><p>A document start (if this is the beginning of a document and not afragment)</p></li><li><p>A start tag for the <tt class="literal">&lt;recipe&gt;</tt> element </p></li><li><p>A start tag for the <tt class="literal">&lt;name&gt;</tt> element </p></li><li><p>The piece of text "peanut butter and jellysandwich"</p></li><li><p>An end tag for the <tt class="literal">&lt;name&gt;</tt> element </p></li><li><p>A comment with the text "add picture of sandwichhere"</p></li><li><p>A start tag for the <tt class="literal">&lt;ingredients&gt;</tt> element </p></li><li><p>A start tag for the <tt class="literal">&lt;ingredient&gt;</tt> element </p></li><li><p>The text "Gloppy" </p></li><li><p>A reference to the entity <tt class="literal">trade</tt> </p></li><li><p>The text "brand peanut butter" </p></li><li><p>An end tag for the <tt class="literal">&lt;ingredient&gt;</tt> element</p></li></ol><p>. . . and so on, until the final event -- the end of thedocument -- is reached.</p><p>Somewhere between chopping up a stream into tokens and processing thetokens is a layer one might call a dispatcher. It branches theprocessing depending on the type of token. The code that deals with aparticular token type is called a <em class="emphasis">handler</em>.There could be a handler for start tags, another for character data,and so on. It could be a compound <tt class="literal">if</tt> statement,switching to a subroutine to handle each case. Or, it could be builtinto the parser as a callback dispatcher, as is the case with<tt class="literal">XML::Parser</tt>'s stream mode. If youregister a set of subroutines, one to an event type, the parser callsthe appropriate one for each token as it'sgenerated. Which strategy you use depends on the parser.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">4. Event Streams</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">4.3. The Parser as Commodity</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -