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

📄 ch01_02.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>XML Is Simple with XML::Simple (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="ch01_01.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="ch01_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">1.2. XML Is Simple with XML::Simple</h2><p>Many people, understandably, think of XML as the invention of an evilgenius bent on destroying humanity. The embedded markup, with itsangle brackets and slashes, is not exactly a treat for the eyes. Addto that the business about nested elements, node types, and DTDs, andyou might cower in the corner and whimper for nice, tab-delineatedfiles and a <tt class="literal">split</tt> function.</p><p>Here's a little secret: writing programs to processXML is not hard. A whole spectrum of tools that handle the mundanedetails of parsing and building data structures for you is available,with convenient APIs that get you started in a few minutes. If youreally need the complexity of a full-featured XML application, youcan certainly get it, but you don't have to. XMLscales nicely from simple to bafflingly complex, and if you deal withXML on the simple end of the continuum, you can pick simple tools tohelp you.</p><p>To prove our point, we'll look at a very basicmodule called <tt class="literal">XML::Simple</tt><a name="INDEX-19" />, created by Grant<a name="INDEX-20" />McLean. Withminimal effort up front, you can accomplish a surprising amount ofuseful work when processing XML.</p><p>A typical program reads in an XML document, makes some changes, andwrites it back out to a file. <tt class="literal">XML::Simple</tt> wascreated to automate this process as much as possible. One subroutinecall reads in an XML document and stores it in memory for you, usingnested hashes to represent elements and data. After you make whateverchanges you need to make, call another subroutine to print it out toa file.</p><p>Let's try it out. As with any module, you have tointroduce <tt class="literal">XML::Simple</tt> to your program with a<tt class="literal">use</tt> pragma like this:</p><blockquote><pre class="code">use XML::Simple;</pre></blockquote><p>When you do this, <tt class="literal">XML::Simple</tt> exports twosubroutines into your namespace:</p><dl><a name="INDEX-21" /><dt><b><tt class="literal">XMLin( )</tt></b></dt><dd><p>This subroutine reads an XML document from a file or string andbuilds a data structure to contain the data and element structure. Itreturns a reference to a hash containing the structure.</p></dd><a name="INDEX-22" /><dt><b><tt class="literal">XMLout( )</tt></b></dt><dd><p>Given a reference to a hash containing an encoded document, thissubroutine generates XML markup and returns it as a string of text.</p></dd></dl><p>If you like, you can build the document from scratch by simplycreating the data structures from hashes, arrays, and strings.You'd have to do that if you wanted to create a filefor the first time. Just be careful to avoid using circularreferences, or the module will not function properly.</p><p>For example, let's say your boss is going to sendemail to a group of people using the world-renowned mailing listmanagement application, WarbleSoft SpamChucker. Among its features isthe ability to import and export XML files representing mailinglists. The only problem is that the boss has trouble readingcustomers' names as they are displayed on the screenand would prefer that they all be in capital letters. Your assignmentis to write a program that can edit the XML datafiles to convert justthe names into all caps.</p><p>Accepting the challenge, you first examine the XML files to determinethe style of markup. <a href="ch01_02.htm#perlxml-CHP-1-EX-1">Example 1-1</a> shows such adocument.</p><a name="perlxml-CHP-1-EX-1" /><div class="example"><h4 class="objtitle">Example 1-1. SpamChucker datafile </h4><blockquote><pre class="code"> &lt;?xml version="1.0"?&gt; &lt;spam-document version="3.5" timestamp="2002-05-13 15:33:45"&gt; &lt;!-- Autogenerated by WarbleSoft Spam Version 3.5 --&gt; &lt;customer&gt;  &lt;first-name&gt;Joe&lt;/first-name&gt;  &lt;surname&gt;Wrigley&lt;/surname&gt;  &lt;address&gt;    &lt;street&gt;17 Beable Ave.&lt;/street&gt;    &lt;city&gt;Meatball&lt;/city&gt;    &lt;state&gt;MI&lt;/state&gt;    &lt;zip&gt;82649&lt;/zip&gt;  &lt;/address&gt;  &lt;email&gt;joewrigley@jmac.org&lt;/email&gt;  &lt;age&gt;42&lt;/age&gt; &lt;/customer&gt; &lt;customer&gt;  &lt;first-name&gt;Henrietta&lt;/first-name&gt;  &lt;surname&gt;Pussycat&lt;/surname&gt;   &lt;address&gt;    &lt;street&gt;R.F.D. 2&lt;/street&gt;    &lt;city&gt;Flangerville&lt;/city&gt;    &lt;state&gt;NY&lt;/state&gt;    &lt;zip&gt;83642&lt;/zip&gt;   &lt;/address&gt;   &lt;email&gt;meow@263A.org&lt;/email&gt;   &lt;age&gt;37&lt;/age&gt;  &lt;/customer&gt; &lt;/spam-document&gt;</pre></blockquote></div><p>Having read the <tt class="literal">perldoc</tt> page describing<tt class="literal">XML::Simple</tt>, you might feel confident enough tocraft a little script, shown in <a href="ch01_02.htm#perlxml-CHP-1-EX-2">Example 1-2</a>.</p><a name="perlxml-CHP-1-EX-2" /><div class="example"><h4 class="objtitle">Example 1-2. A script to capitalize customer names </h4><blockquote><pre class="code"># This program capitalizes all the customer names in an XML document# made by WarbleSoft SpamChucker.# Turn on strict and warnings, for it is always wise to do so (usually)use strict;use warnings;# Import the XML::Simple moduleuse XML::Simple;# Turn the file into a hash reference, using XML::Simple's "XMLin"# subroutine.# We'll also turn on the 'forcearray' option, so that all elements# contain arrayrefs.my $cust_xml = XMLin('./customers.xml', forcearray=&gt;1);# Loop over each customer sub-hash, which are all stored as in an# anonymous list under the 'customer' keyfor my $customer (@{$cust_xml-&gt;{customer}}) {  # Capitalize the contents of the 'first-name' and 'surname' elements  # by running Perl's built-in uc( ) function on them  foreach (qw(first-name surname)) {    $customer-&gt;{$_}-&gt;[0] = uc($customer-&gt;{$_}-&gt;[0]);  }}# print out the hash as an XML document again, with a trailing newline# for good measureprint XMLout($cust_xml);print "\n";</pre></blockquote></div><p>Running the program (a little trepidatious, perhaps, since the databelongs to your boss), you get this output:</p><blockquote><pre class="code">&lt;opt version="3.5" timestamp="2002-05-13 15:33:45"&gt;  &lt;customer&gt;    &lt;address&gt;      &lt;state&gt;MI&lt;/state&gt;      &lt;zip&gt;82649&lt;/zip&gt;      &lt;city&gt;Meatball&lt;/city&gt;      &lt;street&gt;17 Beable Ave.&lt;/street&gt;    &lt;/address&gt;    &lt;first-name&gt;JOE&lt;/first-name&gt;    &lt;email&gt;i-like-cheese@jmac.org&lt;/email&gt;    &lt;surname&gt;WRIGLEY&lt;/surname&gt;    &lt;age&gt;42&lt;/age&gt;  &lt;/customer&gt;  &lt;customer&gt;    &lt;address&gt;      &lt;state&gt;NY&lt;/state&gt;      &lt;zip&gt;83642&lt;/zip&gt;      &lt;city&gt;Flangerville&lt;/city&gt;      &lt;street&gt;R.F.D. 2&lt;/street&gt;    &lt;/address&gt;    &lt;first-name&gt;HENRIETTA&lt;/first-name&gt;    &lt;email&gt;meowmeow@augh.org&lt;/email&gt;    &lt;surname&gt;PUSSYCAT&lt;/surname&gt;    &lt;age&gt;37&lt;/age&gt;  &lt;/customer&gt;&lt;/opt&gt;</pre></blockquote><p>Congratulations! You've written an XML-processingprogram, and it worked perfectly. Well, almost perfectly. The outputis a little different from what you expected. For one thing, theelements are in a different order, since hashesdon't preserve the order of items they contain.Also, the spacing between elements may be off. Could this be aproblem?</p><p>This scenario brings up an important point: there is a trade-offbetween simplicity and completeness. As the developer, you have todecide what's essential in your markup and whatisn't. Sometimes the order of elements is vital, andthen you might not be able to use a module like<tt class="literal">XML::Simple</tt>. Or, perhaps you want to be able toaccess processing instructions and keep them in the file. Again, thisis something <tt class="literal">XML::Simple</tt> can'tgive you. Thus, it's vital that you understand whata module can or can't do before you commit to usingit. Fortunately, you've checked with your boss andtested the SpamChucker program on the modified data, and everyone washappy. The new document is close enough to the original to fulfillthe application's requirements.<a href="#FOOTNOTE-1">[1]</a> Consideryourself initiated into processing XML with Perl!</p><blockquote class="footnote"> <a name="FOOTNOTE-1" /><p>[1]Somemight say that, disregarding the changes we made on purpose, the twodocuments are <em class="emphasis">semantically equivalent</em>, butthis is not strictly true. The order of elements changed, which issignificant in XML. We can say for sure that the documents are closeenough to satisfy all the requirements of the software for which theywere intended and of the end user. </p> </blockquote><p>This is only the beginning of your journey. Most of the book stilllies ahead of you, chock full of tips and techniques to wrestle withany kind of XML. Not every XML problem is as simple as the one wejust showed you. Nevertheless, we hope we've madethe point that there's nothing innately complex orscary about banging XML with your<a name="INDEX-23" /> Perl hammer.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch01_01.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="ch01_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">1. Perl and XML</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">1.3. XML Processors</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 + -