Date: Mon, 19 Apr 1999 15:02:27 -0400 (EDT) From: David Megginson <david@megginson.com> To: Perl-XML Mailing List <perl-xml@listserv.activestate.com> Subject: XML::Writer 0.1 available
I needed this, so I wrote it this morning:
http://www.megginson.com/Software/XML-Writer-0.1.tar.gz
(I've also sent an upload to CPAN, but it might take a while to appear). Here's a very simple synopsis:
use XML::Writer; use IO; my $output = new IO::File(">output.xml"); my $writer = new XML::Writer($output); $writer->startTag("greeting", "class" => "simple"); $writer->characters("Hello, world!"); $writer->endTag("greeting"); $writer->end(); $output->close();
(You can also leave out the $output argument if you want to go straight to STDOUT.)
By default, the module does a fair bit of well-formedness checking to help you catch bugs in your Perl programs -- you can turn the checking off for production use if you like to live on the wild side.
Here are the errors that the module catches so far:
Lack of a (top-level) document element, or multiple document elements.
Unclosed start tags.
Misplaced delimiters in the contents of processing instructions or comments.
Misplaced or duplicate XML declaration(s).
Misplaced or duplicate DOCTYPE declaration(s).
Mismatch between the document type name in the DOCTYPE declaration and the name of the document element.
Mismatched start and end tags.
Attempts to insert character data outside the document element.
Duplicate attributes with the same name.
Full POD documentation and a lot of test cases in test.pl are included.
Enjoy!
[...] Benjamin Holzman writes: > This seems to duplicate the functionality in my XML::Generator, > more or less. Did you look at that module and decide it wasn't > appropriate? I took a quick glance at the README, test.pl, and Generator.pm -- XML::Generator uses a very interesting approach, but (at least as far as I read) it is quite different from the approach that I took with XML::Writer. XML::Generator seems to require that the entire structure be held in memory at once, as hash or array references or what-have-you. While that approach can be very powerful and expressive, it is not suitable for dealing with very large documents that cannot be held in memory all at once. XML::Writer is designed to align very closely with the (important) callbacks in the Handler flavour of XML::Parser, so that it's simple to write an identity transform: use XML::Parser; use XML::Writer; my $writer = new XML::Writer(); sub start { my ($expat, $name, @atts) = (@_); $writer->startTag($name, @atts); } sub end { my ($expat, $name) = (@_); $writer->endTag($name); } sub char { my ($expat, $data) = (@_); $writer->characters($data); } my $parser = new XML::Parser(Handlers => {Start => \&start, End => \&end, Char => \&char}); $parser->parsefile(shift); $writer->end(); Apologies if I have misinterpreted XML::Generator. David
David Megginson david@megginson.com http://www.megginson.com/