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

📄 ch09_04.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>SOAP::Lite  (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_03.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="ch10_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.4. SOAP::Lite </h2><p>Finally<a name="INDEX-760" />, we come to the category of Perl and XMLsoftware that is so ridiculously abstracted from thebook's topic that it's almost notworth covering, but it's definitely much more worthshowing off. This category describes modules and extensions that aresimilar to the <tt class="literal">XML::RSS</tt> class helper modules; theyhelp you work with a specific variety of XML documents, but setthemselves apart by the level of aggression they employ to keepprogrammers separated from the raw, element-encrusted data flowingunderneath it. They involve enough layers of abstraction to make youforget that you're even dealing with XML in thefirst place.</p><p>Of course, they're perfectly valid in doing so; forexample, if we want to write a program that uses theSOAP<a name="INDEX-761" /> <a name="INDEX-762" /> or XML-RPC protocols touse remote code, nothing could be further from our thoughts than XML.It's all a magic carpet, as far aswe're concerned -- we just want our program towork! (And when we do care, a good module lets us peek at the rawXML, if we insist.)</p><p>The Simple Object Access Protocol (SOAP) gives you the power ofobject-oriented web services<a href="#FOOTNOTE-33">[33]</a> by letting you construct and useobjects whose class definitions exist at the other end of a URI. Youdon't even need to know what programming languagethey use because the protocol magically turns theobject's methods into a common, XML-based API. Aslong as the class is documented somewhere, with more details of theavailable class and object methods, you can hack away as if the classwas simply another file on your hard drive, despite the fact that itactually exists on a remote machine.</p><blockquote class="footnote"> <a name="FOOTNOTE-33" /><p>[33]Despite the name, webservices don't have to involve the World Wide Webper se; a web service is simply a piece of software that listenspatiently on a port to which a URI points, and, upon receiving arequest, concocts a reply that makes sense to the requesting entity.A plain old HTTP-trafficking web server is the most common sort ofweb service, but the concept's more recent hypecenters around its newfound ability to provide persistent access toobjects and procedures (so that a programmer can use bits of codethat exist on remote servers, tying them seamlessly into locallystored software).</p> </blockquote><p>At this point it's entirely too easy to forget thatwe're working with XML. At least with RSS, themethod names of the object API more or less match those of theresulting output document; in this case, We don'teven want to see the horrible machine-readable-only document any morethan we'd want to see the numeric codes representingkeystrokes that are sent to our machine's CPU.</p><p><tt class="literal">SOAP::Lite</tt>'s name refers to theamount of work you have to apply when you wish to use it, and doesnot reflect its own weight. When you install it on your system, itmakes a long list of Perl packages available to you, many of whichprovide a plethora of transportation styles,<a href="#FOOTNOTE-34">[34]</a> a <tt class="literal">mod_perl</tt> module to assistwith SOAPy web serving, and a whole lot of documentation andexamples. Then it does most of this all over again with a set ofmodules providing similar APIs for XML-RPC, SOAP'snon-object-oriented cousin.<a href="#FOOTNOTE-35">[35]</a><tt class="literal">SOAP::Lite</tt> is one of those seminal all-singing,all-dancing tools for Perl programmers, doing for web serviceprogramming what CGI.pm does for dynamic web site programming.</p><blockquote class="footnote"> <a name="FOOTNOTE-34" /><p>[34]HTTP isthe usual way to SOAP objects around, but if you want to use raw TCP,SMTP, or even Jabber, <tt class="literal">SOAP::Lite</tt> is ready foryou</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-35" /><p>[35]And whose relationshipwith Perl is covered in depth inO'Reilly's <em class="citetitle">ProgrammingWeb Services with XML-RPC</em> by Simon St.Laurent, JoeJohnston, and Edd Dumbill.</p> </blockquote><p>Let's get our hands dirty with SOAP. </p><a name="perlxml-CHP-9-SECT-4.1" /><div class="sect2"><h3 class="sect2">9.4.1. First Example: A Temperature Converter </h3><p>Every book about programming needs some temperature-conversion codein it somewhere, right? Well, we don't quite havethat here. In this example, lovingly ripped off from the<tt class="literal">SYNOPSIS</tt> section of the documentation for<tt class="literal">SOAP::Lite</tt>, we write a program whose mainfunction, <tt class="literal">f2c</tt>, lives on whatever machine answersto the URI <a href="http://services.soaplite.com/Temperatures">http://services.soaplite.com/Temperatures</a>.</p><blockquote><pre class="code">         use SOAP::Lite;         print SOAP::Lite           -&gt; uri('http://www.soaplite.com/Temperatures')           -&gt; proxy('http://services.soaplite.com/temper.cgi')           -&gt; f2c(32)           -&gt; result;</pre></blockquote><p>Executing this program as a Perl script (on a machine with<tt class="literal">SOAP::Lite</tt> properly installed) gives the correctresponse: <tt class="literal">0</tt>.</p></div><a name="perlxml-CHP-9-SECT-4.2" /><div class="sect2"><h3 class="sect2">9.4.2. Second Example: An ISBN Lookup Engine </h3><p>This example, which uses a little module residing on one of theauthor's personal web servers, is somewhat moreobject oriented. It takes an ISBN number and returns Dublin Core XMLfor almost any book that might match it:</p><blockquote><pre class="code">  my ($isbn_number) = @ARGV;  use SOAP::Lite +autodispatch=&gt;      uri=&gt;'http://www.jmac.org/ISBN',      proxy=&gt;'http://www.jmac.org/projects/bookdb/isbn/lookup.pl';  my $isbn_obj = ISBN-&gt;new;  # The 'get_dc' method fetches Dublin Core information  my $result = $isbn_obj-&gt;get_dc($isbn_number);</pre></blockquote><p>The magic here is that the module on the host machine,<em class="emphasis">ISBN.pm</em>, isn't unusual in anyway; it's a pretty straightforward Perl module thatyou could use in the usual fashion, if you happened to have a localcopy. In other words, we can get the same results by logging into themachine and hammering out a little program like this:</p><blockquote><pre class="code">  my ($isbn_number) = @ARGV;  use ISBN; # This line replaces the long 'use SOAP::Lite' line  my $isbn_obj = ISBN-&gt;new;  # The 'get_dc' method fetches Dublin Core information  my $result = $isbn_obj-&gt;get_dc($isbn_number);</pre></blockquote><p>But, by invoking <tt class="literal">SOAP::Lite</tt> and mumbling a fewextra incantations to aim our sights at a remote machinethat's listening for SOAP-ish requests, youdon't need a copy of that Perl module on your end toenjoy the benefits of its API. And, if we eventually went insane andreimplemented the module in Java, you'd probablynever know it, since we'd keep the interface thesame. In the language-independent world of web services,that's all that matters.</p><p>Where is the XML? We can switch on a valve and peek at the raw stuffroaring beneath this pleasant veneer. Let's see whatactually happens with that ISBN class constructor call after weactivate <tt class="literal">SOAP::Lite</tt>'s<tt class="literal">outputxml</tt> option:</p><blockquote><pre class="code">  my ($isbn_number) = @ARGV;  use SOAP::Lite +autodispatch=&gt;      uri=&gt;'http://www.jmac.org/ISBN', outputxml=&gt;1,      proxy=&gt;'http://www.jmac.org/projects/bookdb/isbn/lookup.pl';  my $isbn_xml = ISBN-&gt;new;  print "$isbn_xml\n";</pre></blockquote><p>What we get back is something like this: </p><blockquote><pre class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:namesp1="http://www.jmac.org/ISBN"&gt;&lt;SOAP-ENV:Body&gt;&lt;namesp2:newResponse xmlns:namesp2="http://www.jmac.org/ISBN"&gt;&lt;ISBN xsi:type="namesp1:ISBN"/&gt;&lt;/namesp2:newResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;</pre></blockquote><p>The second bit of example code had to stop short, of course, since itreturned a scalar containing a pile of XML (which we then<tt class="literal">print</tt>ed) instead of an object belonging to the<tt class="literal">SOAP::Lite</tt> class family. We can'twell continue calling methods on it. We can fix this problem bypassing the blob to the magic <tt class="literal">SOAP::Deserializer</tt>class, which turns SOAPy XML back into objects:</p><blockquote><pre class="code"># Continuing from the previous snippet...my $deserial = SOAP::Deserializer-&gt;new;my $isbn_obj = $deserial-&gt;deserialize($isbn_xml);# Now we can continue as with the first example.</pre></blockquote><p>A little extra work, then, nets us the raw XML as well as the blackboxes of the <tt class="literal">SOAP::Lite</tt> objects. As you mayexpect, this feature has uses far beyond interesting book examples,as getting the raw XML in hand opens up the door to all kinds ofinteresting mischief on our end.</p><p>While <tt class="literal">SOAP::Lite</tt> the Perl module is magic indiverse ways, SOAP the protocol is just, well, a protocol, and allthe strange namespaces, elements, and attributes seen in the XMLgenerated by this module are compliant to the world-readable SOAPspecification.<a href="#FOOTNOTE-36">[36]</a> This compliance allows you to apply a cunning plan toyour SOAP-using application, with which you let the<tt class="literal">SOAP::Lite</tt> module do its usual magic -- butthen your program leaps in, captures the raw XML, does somethingstrange and wonderful with it (it can be parsed with any methodwe've covered so far), and then perhaps returncontrol back to <tt class="literal">SOAP::Lite</tt>.</p><blockquote class="footnote"> <a name="FOOTNOTE-36" /><p>[36]For Version 1.2, see <a href="http://www.w3.org/TR/soap12-part1/">http://www.w3.org/TR/soap12-part1/</a>.</p></blockquote><p>Admittedly, most of <tt class="literal">SOAP::Lite</tt>doesn't require a fingernail'swidth of knowledge about XML processing in Perl, as most applicationswill probably be content with its prepackaged functionality. If youwant to get really tricky with it, though, it welcomes your meddling.Knowledge is power, my friend.</p><p>That's all for our sampling of Perl and XMLapplications. Next, we'll talk about some strategiesfor building our<a name="INDEX-763" /> own applications.</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_03.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="ch10_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.3. XML Programming Tools </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">10. Coding Strategies</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 + -