XML::Simple Parsing with Attributes problem

Discussion in 'Perl Misc' started by John, Feb 3, 2006.

  1. John

    John Guest

    Hi - problem parsing with an attribute name.

    <england>
    <town code="LON">Capital City</town>
    <town code="MAN">Manchester</town>
    <town code="BHAM">Birmingham</town>
    </england>

    I'm using XML::Simple.

    $xml = new XML::Simple (ForceArray=>1, suppressempty=>1);
    $data = $xml->XMLin($xmlfile);

    $which=$data->{england}->[0]->{town}->[0]; doesn't work.

    I need to access both attribute value and its contents (BHAM and
    Birmingham).

    Any ideas?

    John
    John, Feb 3, 2006
    #1
    1. Advertising

  2. John

    John Guest

    Many thanks. I had forgoten about the Dumper.

    I had an *array * of hashes.

    Corrected line should be (in case anybody else reads the thread) as
    follows:-

    $key=$data->{england}->[0]->{town}->[$i]{code};
    $value=$data->{england}->[0]->{town}->[$i]{contents};

    for $i = 1 to num.

    Regards
    John



    "Bernard El-Hagin" <> wrote in
    message news:Xns975F8471CAEFDelhber1lidotechnet@10.232.40.227...
    > "John" <> wrote:
    >
    >> Hi - problem parsing with an attribute name.
    >>
    >> <england>
    >> <town code="LON">Capital City</town>
    >> <town code="MAN">Manchester</town>
    >> <town code="BHAM">Birmingham</town>
    >> </england>
    >>
    >> I'm using XML::Simple.
    >>
    >> $xml = new XML::Simple (ForceArray=>1, suppressempty=>1);
    >> $data = $xml->XMLin($xmlfile);
    >>
    >> $which=$data->{england}->[0]->{town}->[0]; doesn't work.
    >>
    >> I need to access both attribute value and its contents (BHAM and
    >> Birmingham).
    >>
    >> Any ideas?

    >
    >
    > Print out the structure XML::Simple creates (using Data::Dumper) and see
    > for yourself. I suspect it
    > should be something along the lines of
    >
    > $data->{'england'}->{'town'}->[0]->{'code'}
    >
    > to get the code of the first town.
    >
    >
    > --
    > Cheers,
    > Bernard
    John, Feb 3, 2006
    #2
    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. phantom
    Replies:
    2
    Views:
    345
    Lutz Horn
    May 11, 2004
  2. Andy Carson
    Replies:
    8
    Views:
    378
  3. Replies:
    3
    Views:
    2,796
  4. John
    Replies:
    1
    Views:
    121
  5. Erik Wasser
    Replies:
    5
    Views:
    436
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page