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

📄 ch09_03.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>XML Programming Tools  (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="ch09_02.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="ch09_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.3. XML Programming Tools </h2><p>Now<a name="INDEX-742" /> <a name="INDEX-743" /> <a name="INDEX-744" /> we'll coversoftware that performs a somewhat inverse role compared to the groundwe just covered. Instead of giving you Perl-lazy ways to work withXML documents, it uses XML standards to make things easier for a taskthat doesn't explicitly involve XML. Recently, somekey folk in the community from the<em class="emphasis">perl-xml</em><a name="INDEX-745" /> mailing list have been seeking amini-platform of universal data handling in Perl with SAX at itscore. Some very interesting (and useful) examples have been born fromthis research, including Ilya <a name="INDEX-746" />Sterin's<tt class="literal">XML::SAXDriver::Excel</tt> and<tt class="literal">XML::SAXDriver::CSV</tt>, and Matt<a name="INDEX-747" />Sergeant's<tt class="literal">XML::Generator::DBI</tt>. All three modules share theability to take a data format -- Microsoft Excel files,Comma-Separated Value files, and SQL databases,respectively -- and wrap a SAX API around it (the same sortcovered in <a href="ch05_01.htm">Chapter 5, "SAX"</a>, so that any programmer canmerrily pretend that the format is as well behaved and manageable asall the other XML documents they've seen (even ifthe underlying module is quietly performing acrobatics akin tomedicating cats).</p><p>We'll look more closely at one of these tools, asits subject matter has some interesting implications involving recentdevelopments, before we move on to this chapter'sfinal section.</p><a name="perlxml-CHP-9-SECT-3.1" /><div class="sect2"><h3 class="sect2">9.3.1. XML::Generator::DBI </h3><p><tt class="literal">XML::Generator::DBI</tt><a name="INDEX-748" /> is a fine example of a <em class="emphasis">gluemodule</em>, a simple piece of software whose only job is totake two existing (but not entirely unrelated) pieces of software andlet them talk to one another. In this case, when you construct anobject of this class, you hand it your additional objects: aDBI-flavored database handle and a SAX-speaking handler object.</p><p><tt class="literal">XML::Generator::DBI</tt> does not know or care how orwhere the objects came from, but only trusts that they respond to thestandard method calls of their respective families (either DBI, SAX,or SAX2). Then you can call an <tt class="literal">execute</tt> method onthe <tt class="literal">XML::Generator::DBI</tt> object with an ordinarySQL statement, much as you would with a DBI-created database handle.</p><p>The following example shows this module in action. The SAX handler inquestion is an instance of Michael Koehne's<tt class="literal">XML::Handler::YAWriter</tt><a name="INDEX-749" /> module, a pleasantly configurable modulethat turns SAX events into textual output. Using this program, we canturn, say, a SQL table of CDs into well-formed XML and then have itprinted to standard output:</p><blockquote><pre class="code">#!/usr/bin/perluse warnings;use strict;use XML::Generator::DBI;use XML::Handler::YAWriter;use DBI;my $ya = XML::Handler::YAWriter-&gt;new(AsFile =&gt; "-");my $dbh = DBI-&gt;connect("dbi:mysql:dbname=test", "jmac", "");my $generator = XML::Generator::DBI-&gt;new(                               Handler =&gt; $ya,                               dbh =&gt; $dbh                               );my $sql = "select * from cds";$generator-&gt;execute($sql);</pre></blockquote><p>The result is this:</p><blockquote><pre class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;database&gt; &lt;select query="select * from cds"&gt;  &lt;row&gt;   &lt;id&gt;1&lt;/id&gt;   &lt;artist&gt;Donald and the Knuths&lt;/artist&gt;   &lt;title&gt;Greatest Hits Vol. 3.14159&lt;/title&gt;   &lt;genre&gt;Rock&lt;/genre&gt;  &lt;/row&gt;  &lt;row&gt;   &lt;id&gt;2&lt;/id&gt;   &lt;artist&gt;The Hypnocrats&lt;/artist&gt;   &lt;title&gt;Cybernetic Grandmother Attack&lt;/title&gt;   &lt;genre&gt;Electronic&lt;/genre&gt;  &lt;/row&gt;  &lt;row&gt;   &lt;id&gt;3&lt;/id&gt;   &lt;artist&gt;The Sam Handwich Quartet&lt;/artist&gt;   &lt;title&gt;Handwich a la Yogurt&lt;/title&gt;   &lt;genre&gt;Jazz&lt;/genre&gt;  &lt;/row&gt; &lt;/select&gt;&lt;/database&gt;</pre></blockquote><p>This example isn't very interesting, but it looksgood in print. The point is that we didn't have touse YAWriter. We could have used any SAX handler Perl package on oursystem, including ones we wrote ourselves, and tossed them into themix when baking a new <tt class="literal">XML::Generator::DBI</tt> object.Given the same database table as the example above used, when the<tt class="literal">$genenerator</tt> object's<tt class="literal">execute</tt> method is called, it would act as if ithad just parsed the previous XML document (modulo the whitespace thatYAWriter inserted to make things more human-readable). It would actthis way even though the actual source isn't an XMLdocument at all, but a database table.</p></div><a name="perlxml-CHP-9-SECT-3.2" /><div class="sect2"><h3 class="sect2">9.3.2. Further Ruminations on DBI and SAX </h3><p>While we're on the subject, let'sdigress down the path of <a name="INDEX-750" /> <a name="INDEX-751" />DBI and <a name="INDEX-752" />SAX, which may have more in common thanmutual utility in data management.</p><p>The main reason why the Perl DBI earned its position as thepreeminent Perl database interface involves its architecture. Wheninstalling DBI, one must obtain two separate pieces: DBI.pm containsall the code behind the DBI API and its documentation, but it alonewon't let you drive a database with Perl; you alsoneed at least one DBD module that is suitable to the type of databaseyou plan to use. <a name="INDEX-753" />CPAN has many of these modules to choosefrom, <tt class="literal">DBD::MySQL</tt><a name="INDEX-754" />,<tt class="literal">DBD::Oracle</tt><a name="INDEX-755" />, and<tt class="literal">DBD::Pg</tt><a name="INDEX-756" /> for Postgres. While the programmerinteracts only with the DBI module, feeding it SQL queries andreceiving results from it, the appropriate DBD module communicatesdirectly with the actual database. The DBD module turns the abstractDBI methods into highly specific and platform-dependent databasecommands. It does this far underneath the level at which the DBI userworks, so that any Perl program using DBI will work on any databasefor which somebody has made available a DBD driver.<a href="#FOOTNOTE-32">[32]</a></p><blockquote class="footnote"><a name="FOOTNOTE-32" /><p>[32]Assuming, of course, that the programmer took care not to havethe program rely on any queries unique to a given database.<tt class="literal">$sth-&gt;query('select last_insert_id( ) fromfoo')</tt> might work well when hacking on a MySQL database, butcause your friends using Postgres great pain. ConsultO'Reilly's <em class="emphasis">Programmingthe Perl DBI</em> by Alligator Descartes and Tim Bunce for moreinformation.</p> </blockquote><p>A similar movement is on the ascent in the Perl and XML world, whichstarted in 2001 with the SAX drivers mentioned at the start of thissection and ended up with the <tt class="literal">XML::SAX</tt> module, aSAX2 implementation that works like DBI. Tell it you want a SAXparser, optionally specifying the SAX features yourprogram's gotta have, and it roots around on yoursystem to find the best tool for the job, which it instantiates andhands back to you. Then you plug in the SAX handler package of yourchoice (much as with <tt class="literal">XML::Generator::DBI</tt>) and goto town.</p><p>Instead of a variety of DBD drivers that let you use a standardinterface to pull data from a variety of databases, PerlSAX handlerslet you use a standard interface to pull data from any imaginabledata source. As with DBI, it requires only one intrepid hacker towade through the data format in question, and suddenly other Perlprogrammers with a clue about SAX hacking can find themselves using astandard API to handle this once-alien<a name="INDEX-757" /> <a name="INDEX-758" /> <a name="INDEX-759" /> format.</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="ch09_02.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="ch09_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.2. XML::RSS </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">9.4. SOAP::Lite </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 + -