XML - Parsing

Z

zeelani

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><br>
I am not sure how to read "Marriott" in perl. Does anyone have any
ideas?
Thanks a lot,
Zeelani.
 
J

John Bokma

zeelani said:
But if I have
<Hotel>Marriott</Hotel><br>
I am not sure how to read "Marriott" in perl. Does anyone have any
ideas?

Yes, the part between the elements is char data, so what you can do is
create a global buffer, make it grow with char data, in the start tag you
reset the buffer, and in the end tag you check the name of it (Hotel), and
if it's the right one, in the start buffer should be the data (Marriot).

Note that char data can come in bursts, hence you need a global buffer
that you *add* to (not assign to).

(Note, I recomment not to use CamelCase)
 
U

usenet

zeelani said:
But if I have
<Hotel>Marriott</Hotel><br>
I am not sure how to read "Marriott" in perl. Does anyone have any

It's easy to do this with XML::Simple (my parser of choice) for either
type of data (of course, the structure of the data is completely
different, because the input data is completely different). Observe the
results of this script upon both types of input data:

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper::Simple;
use XML::Simple;

while ( my $xml_string = <DATA> ) {
my $hotel_ref = XMLin($xml_string); #parse it
print Dumper $hotel_ref;
}

__DATA__
<Hotel ID = "123" name = "Marriott"></Hotel>
<Hotel>Marriott</Hotel>
 
Z

zeelani

Hi,
Thanks a lot for all the responses. It is working fine now. I used
XML::parser and used the $_ from text subroutine in Endtag subroutine.
Thanks again,
Zeelani.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top