Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

XML Parsing

by saaz11 (Initiate)
on Feb 01, 2006 at 16:24 UTC ( [id://527096]=perlquestion: print w/replies, xml ) Need Help??

saaz11 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am trying to find out how can I read a value between the start and end tags in an XML file in perl.
If I have <Hotel ID = "123" name = "Marriott"></Hotel>
I can read this using
#!/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
<Hotel>Marriott</Hotel>
I am not sure how to read "Marriott" in perl. Does anyone have any ideas?

Thanks a lot.
Zeelani.

Replies are listed 'Best First'.
Re: XML Parsing
by dragonchild (Archbishop) on Feb 01, 2006 at 16:25 UTC
    Ever tried looking at what $TheText has?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      I tried $TheText but it returns nothing.
Re: XML Parsing
by samtregar (Abbot) on Feb 01, 2006 at 18:09 UTC
    You picked the wrong tool. XML::Parser is probably the worst XML parsing interface on CPAN. Try switching to XML::Simple, which should be more than adequate for your needs.

    -sam

      You picked the wrong tool...

      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

Re: XML Parsing
by GrandFather (Saint) on Feb 01, 2006 at 20:32 UTC

    YOu could also consider 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 $/; <DATA>}); 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__ <all> <Hotel>Marriott</Hotel> <Hotel ID = "123" name = "Savoy"></Hotel> </all>

    Prints:

    Marriott (123) Savoy

    DWIM is Perl's answer to Gödel
Re: XML Parsing
by reneeb (Chaplain) on Feb 02, 2006 at 00:16 UTC
    And with XML::LibXML...

    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"; }

    hotels.xml:
    <all> <Hotel>Marriott</Hotel> <Hotel ID = "123" name = "Savoy"></Hotel> </all>
Re: XML Parsing
by chanio (Priest) on Feb 02, 2006 at 03:56 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://527096]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found