📄 ch13_03.htm
字号:
<html><head><title>Expat Handlers (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><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="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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="ch13_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch13_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">13.3. Expat Handlers</h2><p><a name="INDEX-1818" />Expatis an <a name="INDEX-1819" /><a name="INDEX-1820" />event-basedparser that recognizes parts of the document (such as the start orend tag for an XML element) and calls any handlers registered forthat type of an event. All handlers receive an instance ofXML::Parser::Expat as their first argument.</p><dl><a name="INDEX-1821" /><dt><i>Init (<em class="replaceable"><tt>Expat</tt></em>)</i></dt><dd>Called before parsing starts.</p></dd><a name="INDEX-1822" /><dt><i>Final (<em class="replaceable"><tt>Expat</tt></em>)</i></dt><dd>Called after parsing has finished, but only if no errors occurred.</p></dd><a name="INDEX-1823" /><dt><i>Start (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Element</em> [, <em class="replaceable">Attr</em>, <em class="replaceable">Val</tt></em> [,...]])</i></dt><dd>Generated when an XML start tag is encountered.<em class="replaceable"><tt>Element</tt></em> is the name of the XML elementtype. <em class="replaceable"><tt>Attr</tt></em> and<em class="replaceable"><tt>Val</tt></em> pairs are generated for each attributein the start tag of the element.</p></dd><a name="INDEX-1824" /><dt><i>End (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Element</tt></em>)</i></dt><dd>Generated when an XML end tag or empty tag(<tt class="literal"><foo/></tt>) is encountered.</p></dd><a name="INDEX-1825" /><dt><i>Char (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">String</tt></em>)</i></dt><dd>Generated when non-markup is recognized, with the non-markup sequenceof characters in <em class="replaceable"><tt>String</tt></em> given to thehandler in <tt class="literal">UTF-8</tt>.</p></dd><a name="INDEX-1826" /><dt><i>Proc (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Target</em>, <em class="replaceable">Data</tt></em>)</i></dt><dd>Generated when a processing instruction is recognized.</p></dd><a name="INDEX-1827" /><dt><i>Comment (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Data</tt></em>)</i></dt><dd>Generated when a comment is recognized.</p></dd><a name="INDEX-1828" /><dt><i>CdataStart (<em class="replaceable"><tt>Expat</tt></em>)</i></dt><dd>Called at the start of a CDATA section.</p></dd><a name="INDEX-1829" /><dt><i>CdataEnd (<em class="replaceable"><tt>Expat</tt></em>)</i></dt><dd>Called at the end of a CDATA section.</p></dd><a name="INDEX-1830" /><dt><i>Default (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">String</tt></em>)</i></dt><dd>Called for any characters that aren't tied to aregistered handler, including markup declarations. Whatever theencoding in the original document, the string is returned to thehandler in <tt class="literal">UTF-8</tt>.</p></dd><a name="INDEX-1831" /><dt><i>Unparsed (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Entity</em>, <em class="replaceable">Base</em>, <em class="replaceable">Sysid</em>, <em class="replaceable">Pubid</em>, <em class="replaceable">Notation</tt></em>)</i></dt><dd>Called for a declaration of an unparsed entity.<em class="replaceable"><tt>Entity</tt></em> is the name of the entity.<em class="replaceable"><tt>Base</tt></em> is the base to be used for resolvinga relative URI. <em class="replaceable"><tt>Sysid</tt></em> is the system ID.<em class="replaceable"><tt>Pubid</tt></em> is the public ID.<em class="replaceable"><tt>Notation</tt></em> is the notation name.<em class="replaceable"><tt>Base</em> and <em class="replaceable">Pubid</tt></em>can be undefined.</p></dd><a name="INDEX-1832" /><dt><i>Notation (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Notation</em>, <em class="replaceable">Base</em>, <em class="replaceable">Sysid</em>, <em class="replaceable">Pubid</tt></em>)</i></dt><dd>Called for a declaration of notation.<em class="replaceable"><tt>Notation</tt></em> is the notation name.<em class="replaceable"><tt>Base</tt></em> is the base to be used for resolvinga relative URI. <em class="replaceable"><tt>Sysid</tt></em> is the system ID.<em class="replaceable"><tt>Pubid</tt></em> is the public ID.<em class="replaceable"><tt>Base</em>, <em class="replaceable">Sysid</tt></em>,and <em class="replaceable"><tt>Pubid</tt></em> can all be undefined.</p></dd><a name="INDEX-1833" /><dt><i>ExternEnt (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Base</em>, <em class="replaceable">Sysid</em>, <em class="replaceable">Pubid</tt></em>)</i></dt><dd>Called when an external entity is referenced.<em class="replaceable"><tt>Base</tt></em> is the base to be used for resolvinga relative URI. <em class="replaceable"><tt>Sysid</tt></em> is the system ID.<em class="replaceable"><tt>Pubid</tt></em> is the public ID.<em class="replaceable"><tt>Base</em> and <em class="replaceable">Pubid</tt></em>may be undefined. This handler should either return a string or anopen filehandle that represents the contents of the external entity.A return value of <tt class="literal">undef</tt> indicates that theexternal entity couldn't be found.</p><p>If an open filehandle is returned, it must be returned as either aglob or as a reference to a glob (e.g., an instance of IO::Handle).The default handler installed for this event isXML::Parser::lwp_ext_ent_handler unless NoLWP option is true, inwhich case XML::Parser::file_ext_ent_handler is the default handlerfor external entities.</p></dd><a name="INDEX-1834" /><dt><i>ExternEntFin (<em class="replaceable"><tt>Expat</tt></em>)</i></dt><dd>Called after parsing an external entity unless no ExternEnt handleris set.</p></dd><a name="INDEX-1835" /><dt><i>Entity (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Name</em>, <em class="replaceable">Val</em>, <em class="replaceable">Sysid</em>, <em class="replaceable">Pubid</em>, <em class="replaceable">Ndata</em>, <em class="replaceable">IsParam</tt></em>)</i></dt><dd>Called when an entity is declared. For internal entities,<tt class="literal">Val</tt> will contain the value, and the remainingthree parameters will be undefined. For external entities,<em class="replaceable"><tt>Val</tt></em> will be undefined, and the<em class="replaceable"><tt>Sysid</em>, <em class="replaceable">Pubid</tt></em>,and the <em class="replaceable"><tt>Ndata</tt></em> parameters will bepopulated. The <em class="replaceable"><tt>IsParam</tt></em> parameter is set totrue if this is a parameter entity declaration.</p><p>If both this handler and the Unparsed handler are set, then thishandler will not be called for unparsed entities.</p></dd><a name="INDEX-1836" /><dt><i>Element (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Name</em>, <em class="replaceable">Model</tt></em>)</i></dt><dd>Called when an element declaration is found.<em class="replaceable"><tt>Name</tt></em> is the element name, and<em class="replaceable"><tt>Model</tt></em> is the content model as anXML::Parser::Content object.</p></dd><a name="INDEX-1837" /><dt><i>Attlist (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Elname</em>, <em class="replaceable">Attname</em>, <em class="replaceable">Type</em>, <em class="replaceable">Default</em>, <em class="replaceable">Fixed</tt></em>)</i></dt><dd>Called for each attribute in an ATTLIST declaration. An ATTLIST withmultiple attributes will generate multiple calls to Attlist.<em class="replaceable"><tt>Elname</tt></em> is the name of the element withwhich the attribute is being associated.<em class="replaceable"><tt>Attname</tt></em> is the name of the attribute.<em class="replaceable"><tt>Type</tt></em> is the attribute type, given as astring. <em class="replaceable"><tt>Default</tt></em> is the default value,which will be <tt class="literal">#REQUIRED</tt>,<tt class="literal">#IMPLIED</tt>, or a quoted string (i.e., the returnedstring will begin and end with a quote character). If<em class="replaceable"><tt>Fixed</tt></em> is true, then this is a fixedattribute.</p></dd><a name="INDEX-1838" /><dt><i>Doctype (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Name</em>, <em class="replaceable">Sysid</em>, <em class="replaceable">Pubid</em>, <em class="replaceable">Internal</tt></em>)</i></dt><dd>Called for DOCTYPE declarations. <em class="replaceable"><tt>Name</tt></em> isthe document type name. <em class="replaceable"><tt>Sysid</tt></em> is thesystem ID of the document type, if it was provided; otherwise,it's undefined. <em class="replaceable"><tt>Pubid</tt></em> isthe public ID of the document type, which will be undefined if nopublic id was given. <em class="replaceable"><tt>Internal</tt></em> is theinternal subset, given as a string.</p></dd><a name="INDEX-1839" /><dt><i>DoctypeFin (<em class="replaceable"><tt>Parser</tt></em>)</i></dt><dd>Called after parsing of the DOCTYPE declaration has finished,including any internal or external DTD declarations.</p></dd><a name="INDEX-1840" /><dt><i>XMLDecl (<em class="replaceable"><tt>Expat</em>, <em class="replaceable">Version</em>, <em class="replaceable">Encoding</em>, <em class="replaceable">Standalone</tt></em>)</i></dt><dd>Called for XML declarations. <em class="replaceable"><tt>Version</tt></em> is astring containing the version. <em class="replaceable"><tt>Encoding</tt></em> iseither undefined or contains an encoding string.<em class="replaceable"><tt>Standalone</tt></em> is either true orfalse.<a name="INDEX-1841" /></p></dd></dl><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch13_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch13_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">13.2. XML::Parser Methods</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">13.4. XML::Parser Styles</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><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 + -