Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi, you're welcome :)

You made it very easy as your question was very effective (How do I post a question effectively? , How do I compose an effective node title?).

Here is a similar approach using XML::Twig

#!/usr/bin/perl -- #~ 2011-07-23-08:42:20+0000 by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail use Data::Dumper; use XML::Twig; Main( @ARGV ); exit( 0 ); sub Main { Demo(); } sub NotDemoMeaningfulName { my ($xml) = @_; my @data; my %site; my $t = XML::Twig->new( twig_handlers => { '//Site' => sub { warn $_->path; push @data, {%site} if %site; %site = (); $_->purge; # free memory }, '//Site/Id' => sub { warn $_->path; $site{Id} = $_->trimmed_text; }, '//Site/Title' => sub { warn $_->path; $site{Title} = $_->trimmed_text; }, }, ); $t->xparse($xml); $t->purge; return \@data; } ## end sub NotDemoMeaningfulName sub Demo { my $ref = NotDemoMeaningfulName( DemoData() ); print Dumper($ref); } sub DemoData { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=916 +054;part=1 my $xml = <<'__XML__'; <Catalog> <Category> <Name>Arts &amp; Entertainment</Name> <Site> <Id>01-id...</Id> <Title>01-title...</Title> </Site> <Site> <Id>02-id...</Id> <Title>02-title...</Title> </Site> </Category> </Catalog> __XML__ return $xml; } ## end sub DemoData __END__ $ perl xml.twig.916054.pl /Catalog/Category/Site/Id at xml.twig.916054.pl line 30. /Catalog/Category/Site/Title at xml.twig.916054.pl line 34. /Catalog/Category/Site at xml.twig.916054.pl line 24. /Catalog/Category/Site/Id at xml.twig.916054.pl line 30. /Catalog/Category/Site/Title at xml.twig.916054.pl line 34. /Catalog/Category/Site at xml.twig.916054.pl line 24. $VAR1 = [ { 'Id' => '01-id...', 'Title' => '01-title...' }, { 'Id' => '02-id...', 'Title' => '02-title...' } ];

In reply to Re^3: parse xml and store data in array of hashesh by Anonymous Monk
in thread parse xml and store data in array of hashesh by osprofi

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2024-03-28 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found