Fwd: Re: xml::twig

Bryan Dumm bdumm at bobby.bcpub.com
Wed Jan 17 15:07:16 PST 2001


I responded personally, but I thought you 
might be interested. :) I am trying to get
him to join our list. He did though mention 
if we have questions of XML::Twig please
ask......



Hi Bryan,

Waouh! ALFS looks like a cool project.

I just read about everything on the web site, including the source and a
couple of chapters.

I have a couple of comments on the code I saw:

You don't 'use strict',from what I saw the reason seems to be the lines:

if ($valid_gi{$gi}) {
                &{"tag_" . $gi} ($elt);
} else {

You can just apply a "no strict subs;" to this block and you will still be
able to use strict;

Alternatively you could use Twig in a different mode and write
my $twig= new XML::Twig(
                TwigHandlers =>
                  { mkdir  => \&tag_mkdir,
                    mount  => \&tag_mount,
                    link   => \&tag_link,
                    # etc...
                    _default_ => \&error
                  }

All handlers (including error) will be called _during_ parsing (as soon as
the element is completely parsed), with 2 arguments: the twig itself and
the element.

In any case I would write a DTD for each XML file type and run a
validating parser (XML::Checker might be appropriate) before running the
real processing phase, so you are reasonnably sure that your document is
OK. I might actually be able to generate the DTD if you are interested
(but probably not before the end of the month as I am off all of next
week).

As for the " you don't have to escape it in the XML, at the moment
XML::Twig is designed to output XML, hence it escapes quotes (and >, < and
&). You can fight this back by using a character handler:

When you create the twig add an option:
CharHandler => \&char_handler,

Then write a sub
sub char_handler
  { my $string= shift;
    $string=~ s/"/'/g;
    $string=~ s/>/>/;
    $string=~ s/</</;
    $string=~ s/&/&/;
    return $string;
  }

I plan to improve the handling of characters in the next major version of
XML::Twig (BTW I just released 2.02 on CPAN so you can grab it there if
you want), with options to output XML, HTML or text and maybe a choice of
character encoding (or at least hooks to connect your favorite conversion
mechanism).






More information about the alfs-discuss mailing list