Perl and XML.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Perl and XML XML is a text-based markup language that has taken the programming world by storm. More powerful than HTML yet less demanding than SGML, XML has proven itself to be flexible and resilient. XML is the perfect tool for formatting documents with even the smallest bit of complexity, from Web pages to legal contracts to books. However, XML has also proven itself to be indispensable for organizing and conveying other sorts of data as well, thus its central role in web services like SOAP and XML-RPC. As the Perl programming language was tailor-made for manipulating text, few people have disputed the fact that Perl and XML are perfectly suited for one another. The only question has been what's the best way to do it. That's where this book comes in. Perl & XML is aimed at Perl programmers who need to work with XML documents and data. The book covers all the major modules for XML processing in Perl, including XML::Simple, XML::Parser, XML::LibXML, XML::XPath, XML::Writer, XML::Pyx, XML::Parser::PerlSAX, XML::SAX, XML::SimpleObject, XML::TreeBuilder, XML::Grove, XML::DOM, XML::RSS, XML::Generator::DBI, and SOAP::Lite. But this book is more than just a listing of modules; it gives a complete, comprehensive tour of the landscape of Perl and XML, making sense of the myriad of modules, terminology, and techniques. This book covers: • parsing XML documents and writing them out again • working with event streams and SAX • tree processing and the Document Object Model • advanced tree processing with XPath and XSLT Most valuably, the last two chapters of Perl & XML give complete examples of XML applications, pulling together all the tools at your disposal. All together, Perl & XML is the single book that gives you a solid grounding in XML processing with Perl. Table of Contents Preface ......................................................................................................................................... 1 Assumptions ..........................................................................................................................................................1 How This Book Is Organized ................................................................................................................................1 Resources...............................................................................................................................................................2 Font Conventions...................................................................................................................................................2 How to Contact Us ................................................................................................................................................2 Acknowledgments .................................................................................................................................................3 Chapter 1. Perl and XML ............................................................................................................ 4 1.1 Why Use Perl with XML? ...............................................................................................................................4 1.2 XML Is Simple with XML::Simple.................................................................................................................4 1.3 XML Processors ..............................................................................................................................................7 1.4 A Myriad of Modules ......................................................................................................................................8 1.5 Keep in Mind...................................................................................................................................................8 1.6 XML Gotchas ..................................................................................................................................................9 Chapter 2. An XML Recap........................................................................................................ 11 2.1 A Brief History of XML ................................................................................................................................11 2.2 Markup, Elements, and Structure ..................................................................................................................13 2.3 Namespaces ...................................................................................................................................................15 2.4 Spacing ..........................................................................................................................................................16 2.5 Entities...........................................................................................................................................................17 2.6 Unicode, Character Sets, and Encodings .......................................................................................................19 2.7 The XML Declaration....................................................................................................................................19 2.8 Processing Instructions and Other Markup....................................................................................................19 2.9 Free-Form XML and Well-Formed Documents ............................................................................................21 2.10 Declaring Elements and Attributes ..............................................................................................................22 2.11 Schemas.......................................................................................................................................................22 2.12 Transformations...........................................................................................................................................24 Chapter 3. XML Basics: Reading and Writing.......................................................................... 28 3.1 XML Parsers..................................................................................................................................................28 3.2 XML::Parser ..................................................................................................................................................34 3.3 Stream-Based Versus Tree-Based Processing ...............................................................................................38 3.4 Putting Parsers to Work.................................................................................................................................39 3.5 XML::LibXML..............................................................................................................................................41 3.6 XML::XPath ..................................................................................................................................................43 3.7 Document Validation.....................................................................................................................................44 3.8 XML::Writer..................................................................................................................................................46 3.9 Character Sets and Encodings........................................................................................................................50 Chapter 4. Event Streams .......................................................................................................... 55 4.1 Working with Streams ...................................................................................................................................55 4.2 Events and Handlers ......................................................................................................................................55 4.3 The Parser as Commodity..............................................................................................................................57 4.4 Stream Applications.......................................................................................................................................57 4.5 XML::PYX ....................................................................................................................................................58 4.6 XML::Parser ..................................................................................................................................................60 Chapter 5. SAX.......................................................................................................................... 64 5.1 SAX Event Handlers......................................................................................................................................64 5.2 DTD Handlers................................................................................................................................................70 5.3 External Entity Resolution.............................................................................................................................73 5.4 Drivers for Non-XML Sources......................................................................................................................74 5.5 A Handler Base Class ....................................................................................................................................76