XML, the Perl Way

Previous
2. Introduction to XML
Table of Content
Table of Content
Next
4. First Examples

3. Introduction to XML::Twig

3.1 XML::Parser

XML::Parser, first developped by Larry Wall and now supported by Clark Cooper, is the basis of most other XML modules. It includes a non-validating parser, Expat, written by James Clark, who amongst other feats also wrote the nsgmls parser for SGML.

XML::Parser allows calling software to set handlers on parsing events. Those events include start tags (and XML::Parser gives the name of the tag and the attributes), end tags, text, processing instructions etc...

3.2 XML::Twig

XML::Twig is a sub-class of XML::Parser that allows higher level processing of XML. XML::Twig offers a tree interface to a document, both once the document has been completely parsed and during the parsing by allowing handlers to be defined on elements. Additional methods help managing the resources needed by XML::Twig.

A whole bunch of methods can be used on elements in the twig, to navigate it, transform it, create new elements...

3.3 Why use XML::Twig

XML:Twig is only one of the dozen or so Perl modules that process XML. Other popular ones are XML::DOM, XML::Simple, XML::PYX, XML::Grove or just plain vanilla XML::Parser.

So why would you use XML::Twig?

XML::Twig uses a tree-based processing model, you can control how much of the tree you want to load at once in memory and it is very perlish, up to TIMTOWTDI and DWIM.


Previous
2. Introduction to XML
Table of Content
Table of Content
Next
4. First Examples