XML - Parsing

Discussion in 'Perl Misc' started by zeelani, Feb 1, 2006.

  1. zeelani

    zeelani Guest

    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.
     
    zeelani, Feb 1, 2006
    #1
    1. Advertising

  2. zeelani

    John Bokma Guest

    "zeelani" <> wrote:

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

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Feb 1, 2006
    #2
    1. Advertising

  3. zeelani

    Guest

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

    --
    http://DavidFilmer.com
     
    , Feb 1, 2006
    #3
  4. zeelani

    zeelani Guest

    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.
     
    zeelani, Feb 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Per Magnus L?vold
    Replies:
    0
    Views:
    1,388
    Per Magnus L?vold
    Nov 15, 2004
  2. Greg Wogan-Browne
    Replies:
    1
    Views:
    814
    Uche Ogbuji
    Jan 28, 2005
  3. Replies:
    2
    Views:
    503
  4. John Levine
    Replies:
    0
    Views:
    733
    John Levine
    Feb 2, 2012
  5. Erik Wasser
    Replies:
    5
    Views:
    465
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page