It's better to give XML::Simple a few hints so that it creates a datastructure that's consistent and fine to work with.

use strict; use warnings; use XML::Simple; my $xml_raw = <<XML_RAW; <survey> ... </survey> XML_RAW my $data = XMLin($xml_raw, ForceArray => [qw(river fish)], KeyAttr => +[]); foreach my $Animal (@{$data->{animals}{fish}}) { print <<"*END*"; [ Survey information for: $Animal->{name} ]: Saltwater:$Animal->{saltwater} Freshwater:$Animal->{freshwater} Rivers covered in survey: *END* for (@{$Animal->{river}}) { print $_, "\n"; } print "\n"; }
or you could use XML::Rules and print the report as the file is being parsed:
use strict; use warnings; use XML::Rules; my $xml_raw = <<XML_RAW; <survey> ... </survey> XML_RAW my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => '', river => 'content array', fish => sub { print <<"*END*"; [ Survey information for: $_[1]->{name} ]: Saltwater:$_[1]->{saltwater} Freshwater:$_[1]->{freshwater} Rivers covered in survey: *END* for (@{$_[1]->{river}}) { print $_, "\n"; } print "\n"; }, } ); $parser->parse($xml_raw);
or
my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => '', river => sub {'.river' => "$_[1]->{_content}\n"}, fish => sub { print <<"*END*"; [ Survey information for: $_[1]->{name} ]: Saltwater:$_[1]->{saltwater} Freshwater:$_[1]->{freshwater} Rivers covered in survey: $_[1]->{river} *END* }, } ); $parser->parse($xml_raw);


In reply to Re: XML & data structure parsing fun (XML::Simple ??) by Jenda
in thread XML & data structure parsing fun (XML::Simple ??) by kabeldag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.