#!/usr/bin/perl
use XML::Parser;
$parser = new XML::Parser(Style => 'Stream');
$parser->parsefile('abc.xml');
print "$Hotel_id\n", "$Hotel_name\n";
sub StartTag {
my $Handler = shift(@_);
my $Name = shift(@_);
my %Attr = %_;
if($Name eq 'Hotel') {
$Hotel_id = $Attr{'id'};
$Hotel_name = $Attr{'name'};
}
sub Text {
$TheText = $_ ;
}
sub EndTag
{
if($Name eq 'Hotel'){
exit(0) };
But if I have
Thanks a lot.
Zeelani.
-sam
I would not make that sort of blanket assertion. Note that XML::Simple is actually a layer on top of XML::Parser -- and the man page for XML::Simple is (paradoxically) much longer and more complicated than the one for XML::Parser.
From my perspective, XML::Simple applies a lot of "default" (or maybe even inescapable) assumptions about what you intend to do with XML data, and if what you really want to do is a different from that... well, it might not be so "Simple".
Lots of good work can be done with XML::Parser, and it's not that hard to master. Maybe it's "the worst XML parsing interface on CPAN" for your purposes, but I'm sure there are many who find it both useful and appropriate for a variety of jobs.
Note that XML::Simple is actually a layer on top of XML::Parser
Sure, but that says nothing about the quality of the interface. XML::Parser is a high-quality, fast XML parser, but programming for it bites.
and the man page for XML::Simple is (paradoxically) much longer and more complicated than the one for XML::Parser.
I pity the fool that chooses modules based on the length of their man page! The only reason XML::Parser's man-page is so short is that it's missing a lot of useful stuff, like working examples. I'd much rather have a simple interface with a long manual than a complex interface with a short one.
it's not that hard to master.
Well, everbody's different, so I guess you might find it easier than most. Hang out on the perl-xml list and I think you'll see a pattern emerge - XML::Parser is pretty hard for most people to use. Almost everything else on CPAN that does the same job is more intuitive.
-sam
YOu could also consider [mod://XML::Twig]. Like Perl it makes simple things simple, and most things possible:
use warnings;
use strict;
use XML::Twig;
my $twig = new XML::Twig;
$twig->parse (do {local $/; });
for ($twig->descendants ('Hotel'))
{
my $Hotel_name = $_->{'att'}{'name'} || $_->text;
next if ! defined $Hotel_name;
my $Hotel_id = $_->{'att'}{'ID'};
print "($Hotel_id) " if defined $Hotel_id;
print "$Hotel_name\n";
}
__DATA__
Marriott
Prints:
Marriott (123) Savoy
use warnings;
use strict;
use XML::LibXML;
my $parser = new XML::LibXML;
my $tree = $parser->parse_file('hotels.xml');
my $root = $tree->getDocumentElement();
for ($root->getElementsByTagName('hotel')){
my $Hotel_name = $_->getAttribute('name') || $_->getData();
next if ! defined $Hotel_name;
my $Hotel_id = $_->getAttribute('ID');
print "($Hotel_id) " if defined $Hotel_id;
print "$Hotel_name\n";
}
Marriott
perlmonks.org content © perlmonks.org and chanio, dragonchild, graff, GrandFather, reneeb, saaz11, samtregar
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03