WebReference.com - Chapter 3 from Perl & XML, from O'Reilly and Associates (1/12)
Perl & XML
Chapter 3: XML Basics: Reading and Writing
[The following excerpt is from chapter 3 of the O'Reilly & Associates title, Perl & XML.]
This chapter covers the two most important tasks in working with XML: reading it into memory and writing it out again. XML is a structured, predictable, and standard data storage format, and as such carries a price. Unlike the line-by-line, make-it-up-as-you-go style that typifies text hacking in Perl, XML expects you to learn the rules of its game--the structures and protocols outlined in Chapter 2--before you can play with it. Fortunately, much of the hard work is already done, in the form of module-based parsers and other tools that trailblazing Perl and XML hackers already created (some of which we touched on in Chapter 1).
Knowing how to use parsers is very important. They typically drive the rest of the processing for you, or at least get the data into a state where you can work with it. Any good programmer knows that getting the data ready is half the battle. We'll look deeply into the parsing process and detail the strategies used to drive processing.
Parsers come with a bewildering array of options that let you configure the output to your needs. Which character set should you use? Should you validate the document or merely check if it's well formed? Do you need to expand entity references, or should you keep them as references? How can you set handlers for events or tell the parser to build a tree for you? We'll explain these options fully so you can get the most out of parsing.
Finally, we'll show you how to spit XML back out, which can be surprisingly tricky if one isn't aware of XML's expectations regarding text encoding. Getting this step right is vital if you ever want to be able to use your data again without painful hand fixing.
- XML Parsers
- Example (of What Not to Do): A Well-Formedness Checker
- Well-Formedness Checker Revisited (using XML::Parser)
- Stream-Based Versus Tree-Based Processing
- Putting Parsers to Work
- XML::LibXML and XML::XPath
- Document Validation
- Character Sets and Encodings
- Encoding Conversion
Created: May 8, 2002
Revised: May 8, 2002