Click here to get back home

XML::Parser examples for the novice

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
XML::Parser examples for the novice lars 11-15-2005
Posted by lars on November 15, 2005, 11:31 am
Please log in for more thread options


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


Posted by John Bokma on November 15, 2005, 12:22 pm
Please log in for more thread options


lars@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 :-)



Posted by lars on November 15, 2005, 7:28 pm
Please log in for more thread options


: <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

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


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 :-)



Posted by Bart Lateur on November 16, 2005, 8:27 am
Please log in for more thread options


lars@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.


Similar ThreadsPosted
DBIx::Threaded examples April 24, 2008, 1:21 pm
File::Find Win32 Examples October 26, 2005, 11:51 am
need help with HTTP-WebTest module, tutorials or examples September 8, 2007, 1:19 am
Need Help with XML::Parser July 5, 2005, 12:53 pm
X500::DN::Parser V 1.16 July 29, 2004, 7:20 am
Can not Install XML::Parser September 29, 2004, 5:26 pm
How to use SGML::Parser? August 2, 2005, 5:18 pm
Possible bug in HTML::Parser November 15, 2005, 5:05 pm
Installing XML Parser May 25, 2006, 2:20 am
XML file parser April 4, 2007, 8:33 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap