Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML::Parser examples for the novice

1 view
Skip to first unread message

la...@nospam.nosoftwarepatents.edu

unread,
Nov 15, 2005, 6:31:50 AM11/15/05
to
What are some good examples or explanations of using XML::Parser?

The man pages don't give me quite enough to get started with and I'm
looking for something to bridge the gap at or closer to the novice level.
the examples and explanations in the man pages are too abbreviated and take
short cuts which (for me) obscure how they are to be used in a different
context.

Specifically, I'd like to strip out processing statments and a few other
elements.

--
Lars
Software patents harm all Net-based business, write your MEP:
http://wwwdb.europarl.eu.int/ep6/owa/p_meps2.repartition?ilg=EN

John Bokma

unread,
Nov 15, 2005, 7:22:07 AM11/15/05
to
la...@nospam.nosoftwarepatents.edu wrote:

> What are some good examples or explanations of using XML::Parser?
>
> The man pages don't give me quite enough to get started with and I'm
> looking for something to bridge the gap at or closer to the novice
> level. the examples and explanations in the man pages are too
> abbreviated and take short cuts which (for me) obscure how they are to
> be used in a different context.
>
> Specifically, I'd like to strip out processing statments and a few
> other elements.

<http://johnbokma.com/perl/finding-unique-xml-elements.html>

However, you might want to look at some of the other XML modules.


--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

la...@nospam.nosoftwarepatents.edu

unread,
Nov 15, 2005, 2:28:47 PM11/15/05
to
John Bokma <jo...@castleamber.com> 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

John Bokma

unread,
Nov 15, 2005, 6:36:29 PM11/15/05
to
la...@nospam.nosoftwarepatents.edu wrote:

> John Bokma <jo...@castleamber.com> 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;

Bart Lateur

unread,
Nov 16, 2005, 3:27:20 AM11/16/05
to
la...@nospam.nosoftwarepatents.edu wrote:

>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

I think XML::TokeParser is a layer on top of XML::Parser. So
technically, you're still using the same parser... ;-)

--
Bart.

0 new messages