XML technologies / XML / Perl & XML / XML basics: Reading and writing
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
This section covers the two most important XML operations: reading a document into memory and writing it back to disk. XML is a structured, deterministic, standardized data format, and that comes at a price. Processing text files with Perl has traditionally followed a line-oriented, exploratory approach. XML, by contrast, requires knowledge of certain rules, structures, and protocols, and strict adherence to them. We encountered this in Introduction to XML. Fortunately, most of the work has already been done. Parser modules and similar tools provided by Perl and XML pioneers give us a solid foundation. We have already met some of these tools in Perl and XML.
Mastering parsers is essential. They normally do most of the work for us, at least delivering the data in a usable form. Every good programmer knows that properly prepared data is half the battle. We will therefore examine parsing closely and describe some common strategies.
Parsers provide many configuration options for adapting their output to your needs. Which character set should I use? Should the document be validated against a DTD, or is well-formednessenough? Should Entities be expanded or their references left intact? How do you set event handlers or tell the parser to build a tree? We will explain these options in turn so you can get the most from your parser.
Finally, we will look at writing XML back out. This can become surprisingly complicated if you ignore XML’s expectations concerning character sets and so on. Pay particular attention to this step if you want to avoid manual corrections afterward.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top