XML, the Perl Way

XML, The Perl Way

Welcome to the XML::Twig Page.

XML::Twig logo XML::Twig is a Perl module used to process efficiently XML documents

Twig offers a tree-oriented interface to a document while still allowing the processing of documents of any size. I think the current buzzword for it would be "push-pull" processing ;--)When I was younger I wanted to grow up and write a tool that would allow people to process text the way they wanted, offering tons of feature, various ways to achieve the same result, not forcing them into any processing model but allowing them to use the one they felt the most comfortable with. Eventually I grew up and I realized a guy named Larry Wall had already written a language named Perl... Darn! So as I was quite involved in dealing with SGML, then XML, I decided to settle for the next best thing: writing a module that would allow people to process XML the way they wanted, offering them tons of feature, various ways... you get the point.So I wrote XML::Twig. XML::Twig gives you a tree interface to XML documents... if you want. It also lets you dump parts of the tree, set callbacks during processing, both on tags and on subtrees, process only part of the tree, write powerful filters... you name it. The only thing XML::Twig does not do is follow standards (except XML of course): no DOM, no XPath (well, not quite, it implements a subset of XPath), no SAX ( it is planned though, as of 3.05 you can output SAX using the toSAX1 and toSAX2 methods). Consider yourself warned!


Latest development versions (Tue Nov 25 2019)

Git repo: mirod's xmltwig master, on github

Latest stable version

XML::Twig page on seach.cpan.org

Mirrors on CPAN



Development version: html or text, Changes

Stable version: html or text, Changes

The XML::Twig Quick Reference Page

You can also have a look at the FAQ (updated Sat Jun 16 06:32:56 2012)

The development version documentation is probably the easiest to use: it is better cross-referenced, methods are sorted alphabetically and you can expand/collapse method lists.


A complete tutorial, covering XML::Twig basics, integrating XML documents with data bases, HTML conversion and more...

Note that I wrote it back in the days of version 1.10 or so so it does not include some of the latest additions to the module. It should get you started though!


The Ways to Rome series of articles has examples of how to use XML::Twig (along with many other modules).

Kip Hampton also wrote an very good article: Using XML::Twig for xml.com.

Tom Anderson has an interesting article: Performance Comparison Between SAX XML::Filter::Dispatcher and XML::Twig. I have improved slightly the XML::Twig version of his test, see my comments.

Finally, I cannot really comment on XML-Twig, by Stas Orlenko, an article in Russian ;--(


A 2 hour introduction to XML::Twig: Growing Twigs, gives a generic discussion on XML processing, how to install XML::Twig, code examples and a quick history of the module.


Finally, you can hear me talk about my favourite subjet in an interview with Josh McAdams on perlcast: Michel Rodriguez on XML::Twig.

Request tracking system:

You can file bug reports and view outstanding bugs throught the XML::Twig bug list, using rt.cpan.org

Tools built with XML::Twig

Test Coverage

The latest coverage.

BTW, a good way to help the development of XML::Twig is to write more tests to increase this coverage. Look at this source and work on getting rid of uncovered branckes (the ones with red numbers). Just make sure the tests are portable accross various OSs and versions of Perl. Coverage is already pretty good but it is always better to increase it.

More (simple) Examples

All the examples: twig_examples.tar.gz

A series of examples on a single xml file

The data is a list of perl modules in ex_fm.xml. To run the examples: ex_fm<n> ex_fm.xml.

An example using 2 documents and $twig->finish_print to speed up processing. To run it: ex_insert insert_new<n>.xml insert_main.xml

And finally: Examples of XML::Twig on Perlmonks, a list of my posts about XML::Twig on PerlMonks.


If you want to support XML::Twig's development, I always appreciate anything from XML::Twig's wishlist on Amazon. Thanks.