|
Posted by John Bokma on November 15, 2005, 11:36 pm
Please log in for more thread options
lars@nospam.nosoftwarepatents.edu wrote:
> : <http://johnbokma.com/perl/finding-unique-xml-elements.html>
>
> : However, you might want to look at some of the other XML modules.
>
> Thanks. That's a step, but I'm still wondering how to weed out
> processing statements and comments (well that part I get) while
> accumulating the everything else into a buffer unmodified. I'll try
> some more variations. Maybe it's just something obvious.
>
> My first thought *was* to use another parser. XML::TokeParser seems
> easier, but since this will be a plug-in for a larger program which
> already uses XML::Parser, I'd like to avoid using another module if
> there's away to do it with XML::parser
XML::Parser is a dinosaur. It's fast, but normally you don't want to use
it directly unless you want to :-). Most wrappers are way more friendly.
I am sure that there are modules that do what you want in just a few
lines. The whole idea of modules is that you use as much as possible, if
that makes your work better :-).
Have a look at XML::Twig:
http://xmltwig.com/xmltwig/twig_stable.html#METHODS_XML_Twig_new_pi "pi
Set the way processing instructions are processed: 'drop', 'keep'
(default) or 'process'"
"comments
Set the way comments are processed: 'drop' (default), 'keep' or
'process'"
If you insist on XML::Parser:
use strict;
use warnings;
use XML::Parser;
my $parser = new XML::Parser(
Style => 'Stream',
Handlers => {
Comment => \&xml_comment,
Proc => \&xml_pi
},
);
$parser->parse( <<'XML' );
<foo>
<?some_processing_instruction?>
<bar>
some text
<!-- comment -->
</bar>
</foo>
XML
sub xml_comment {
return;
}
sub xml_pi {
return;
}
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
|