📄 ch04_01.htm
字号:
<html><head><title>Event Streams (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 & 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 & 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="ch03_09.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h1 class="chapter">Chapter 4. Event Streams</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch04_01.htm">Working with Streams</a><br /><a href="ch04_02.htm">Events and Handlers</a><br /><a href="ch04_03.htm">The Parser as Commodity</a><br /><a href="ch04_04.htm">Stream Applications</a><br /><a href="ch04_05.htm">XML::PYX</a><br /><a href="ch04_06.htm">XML::Parser</a><br /></p></div><p>Now<a name="INDEX-325" /> <a name="INDEX-326" />that you're all warmed up with parsers and haveenough knowledge to make you slightly dangerous,we'll analyze one of the two important styles of XMLprocessing: event streams. We'll look at someexamples that show the basic theory of stream processing and graduatewith a full treatment of the standard Simple API for XML (SAX).</p><div class="sect1"><a name="perlxml-CHP-4-SECT-1" /><h2 class="sect1">4.1. Working with Streams</h2><p>In <a name="INDEX-327" />the world of computer science, a<em class="emphasis">stream</em><a name="INDEX-328" />is a sequence of data chunks to be processed. A file, for example, isa sequence of characters (one or more bytes each, depending on theencoding). A program using this data can open a filehandle to thefile, creating a character stream, and it can choose to read in datain chunks of whatever size it chooses. Streams can be dynamicallygenerated too, whether from another program, received over a network,or typed in by a user. A stream is an abstraction, making the sourceof the data irrelevant for the purpose of processing.</p><p>To summarize, here are a stream's importantqualities:</p><ul><li><p>It consists of a sequence of data fragments. </p></li><li><p>The order of fragments transmitted is significant. </p></li><li><p>The source of data (e.g., file or program output) is not important. </p></li></ul><p>XML streams are just clumpy character streams. Each data clump,called a <em class="emphasis">token</em><a name="INDEX-329" /> in parser parlance, is aconglomeration of one or more characters. Each token corresponds to atype of markup, such as an element start or end tag, a string ofcharacter data, or a processing instruction. It'svery easy for parsers to dice up XML in this way, requiring minimalresources and time.</p><p>What makes XML streams different from character streams is that thecontext of each token matters; you can't just pumpout a stream of random tags and data and expect an XML processor tomake sense of it. For example, a stream of ten start tags followed byno end tags is not very useful, and definitely not well-formed XML.Any data that isn't well-formed will be rejected.After all, the whole purpose of XML is to package data in a way thatguarantees the integrity of a document's structureand labeling, right?</p><p>These contextual rules are helpful to the parser as well as thefront-end processor. XML was designed to be very easy to parse,unlike other markup languages that can require look-ahead orlook-behind. For example, SGML does not have a rule requiringnonempty elements to have an end tag. To know when an element endsrequires sophisticated reasoning by the parser. This requirementleads to code complexity, slower processing speed, and increasedmemory usage.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_09.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.9. Character Sets and Encodings</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.2. Events and Handlers</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 © 2002</a> O'Reilly & 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 + -