📄 ch13_02.htm
字号:
<html><head><title>XML::Parser Methods (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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">13.2. XML::Parser Methods</h2><p><a name="INDEX-1810" />Thefollowing methods are defined by XML::Parser.</p><a name="INDEX-1811" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>new(%<em class="replaceable">option</em>)</pre><p><a name="INDEX-1811" />Constructor. Options are passed askeyword/value pairs. Recognized options are:</p><dl><dt><b><tt class="literal">Style</tt></b></dt><dd>Sets the type of parser. The built-in styles are<tt class="literal">Debug</tt>, <tt class="literal">Subs</tt>,<tt class="literal">Tree</tt>, <tt class="literal">Objects</tt>, and<tt class="literal">Stream</tt>. Custom styles can be provided by giving afully qualified package name containing at least one"::". This package must havesubroutines defined for each parser type.</p></dd><dt><b><tt class="literal">Handlers</tt></b></dt><dd>An anonymous hash containing the handler types as the keys with theirassociated values, which are references to subroutines for handlingthat type of event.</p></dd><dt><b><tt class="literal">Pkg</tt></b></dt><dd>Some styles will refer to subroutines defined in the specifiedpackage.</p></dd><dt><b><tt class="literal">ErrorContext</tt></b></dt><dd>The number of lines to show surrounding the line in which an erroroccurred.</p></dd><dt><b><tt class="literal">ProtocolEncoding</tt></b></dt><dd>Sets the protocol-encoding name (default is none). The built-inencodings are <tt class="literal">UTF-8</tt>,<tt class="literal">ISO-8859-1</tt>, <tt class="literal">UTF-16</tt>, and<tt class="literal">US-ASCII</tt>. You may introduce additional encodingsby adding directories to <tt class="literal">@Encoding_Path</tt>.</p></dd><dt><b><tt class="literal">Namespaces</tt></b></dt><dd>If true, then namespace processing is done during the parse.</p></dd><dt><b><tt class="literal">NoExpand</tt></b></dt><dd>Normally, the parser will try to expand references to entitiesdefined internally. If this option is true and a default handler isalso set, then the default handler will be called when an entityreference is encountered.</p></dd><dt><b><tt class="literal">Stream_Delimiter</tt></b></dt><dd>A string for XML::Parser to interpret as end-of-file if found aloneon a line. Useful when working with MIME multipart documents. Thestring should not contain a trailing newline.</p></dd><dt><b><tt class="literal">ParseParamEnt</tt></b></dt><dd>Unless standalone is set to <tt class="literal">yes</tt> in the XMLdeclaration, setting this to true allows the external DTD to be read,and parameter entities to be parsed and expanded.</p></dd><dt><b><tt class="literal">NoLWP</tt></b></dt><dd>If true, forces the use of a file-based external entity handler.</p></dd><dt><b><tt class="literal">Non-Expat-Options</tt></b></dt><dd>An anonymous hash whose keys are options thatshouldn't be passed to Expat. This should be aconcern only for those subclassing XML::Parser.</p></dd></dl></div><a name="INDEX-1812" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parse</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><a name="INDEX-1812" />parse(<em class="replaceable">source</em> [, <em class="replaceable">opt</em> => <em class="replaceable">opt_value</em> [...]]) </pre><p>Parses the document. The <em class="replaceable">source</em> parametershould be either a string containing the entire XML document or anopen IO::Handle. Options given as keyword/value pairs may follow the<em class="replaceable"><tt>source</tt></em> parameter and will override anyoptions or attributes passed from the XML::Parser instance.</p><p><tt class="literal">parse</tt> exits with <tt class="literal">die</tt> if a parseerror occurs or returns <tt class="literal">1</tt> with success.</p></div><a name="INDEX-1813" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parsefile</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><a name="INDEX-1813" />parsefile(<em class="replaceable">file</em> [, <em class="replaceable">opt</em> => <em class="replaceable">opt_value</em> [...]]) </pre><p>Opens <em class="replaceable">file</em> for reading, then calls<tt class="literal">parse</tt> with the open handle. The file is closed nomatter what parse returns. Returns what <tt class="literal">parse</tt>returns.</p></div><a name="INDEX-1814" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parse_start</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><a name="INDEX-1814" />parse_start([ <em class="replaceable">opt</em> => <em class="replaceable">opt_value</em> [...]]) </pre><p>Creates and returns a new instance of XML::Parser::ExpatNB. If aninit handler is specified, it is called before returning the ExpatNBobject. Documents are parsed with incremental calls to the<tt class="literal">parse_more</tt>. A single call to the<tt class="literal">parse_done</tt> method of this object indicates thatthe document is finished. The <tt class="literal">parse_done</tt> methodreturns whatever is returned by the final handler.</p></div><a name="INDEX-1815" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parsestring</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><a name="INDEX-1815" />parsestring </pre><p>An alias for <tt class="literal">parse</tt>, for backwards compatibility.</p></div><a name="INDEX-1816" /><a name="INDEX-1817" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>setHandlers</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre><a name="INDEX-1816" />setHandlers(<em class="replaceable">type</em>, <em class="replaceable">handler</em> [, <em class="replaceable">type</em>, <em class="replaceable">handler</em> [...]]) </pre><p>Registers handlers for various parser events.<tt class="literal">setHandlers</tt> overrides any previous handlers set in<tt class="literal">Style</tt> or <tt class="literal">Handler</tt> options orthrough earlier calls to <tt class="literal">setHandlers</tt>. Returns alist of type and handler pairs.<a name="INDEX-1817" /> </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="ch13_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">13. XML and Perl</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.3. Expat Handlers</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 + -