Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

data extraction XML::Simple

by jonnyfolk (Vicar)
on Mar 10, 2010 at 21:51 UTC ( #827906=perlquestion: print w/ replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

Trying to organise some diving gear, I'm drawing a blank on extracting the data. Could someone explain where I'm going wrong, please?
$newxml = qq~ <water_sports> <Tenders_+_toys>- 1x 4.45m Tender Williams 445 Sportjet - 1x 3.51m Tender Arimar Top Line 360 - 1xJet ski Yamaha GP1300R - banana/kayak - water skiing/snorkelling equipment </Tenders_+_toys> </water_sports> ~; $newxml =~ s/\+/and/g; my $xml = new XML::Simple; my $data = $xml->XMLin(\$newxml); print $data->{'water_sports'}->{'Tenders_and_toys'};

Comment on data extraction XML::Simple
Download Code
Re: data extraction XML::Simple
by ikegami (Pope) on Mar 10, 2010 at 22:02 UTC
    Use
    use Data::Dumper; print Dumper $data;
    Then notice XML::Simple removes the root node by default. You simply need
    print $data->{Tenders_and_toys};
      Hi hadn't realized that the root node was removed, and you example works, thank you very much. Making string more complex, and trying to take you advice into account I am still drawing a blank!!
      $newxml = qq~ <record> <namepairs> <name>My Name </name> <img>My image</img> <brochure>My Brochure</brochure> </namepairs> </record>~; my $xml = new XML::Simple(ForceArray => 1, KeyAttr => [], KeepRoot => +1, SuppressEmpty => ''); my $data = $xml->XMLin(\$newxml); #print $data->{'name'}; print $data->{'namepairs'}->{'name'}; #print Dumper($data); exit;
        The dump shows
        VAR1 = { 'record' => [ { 'namepairs' => [ { 'brochure' => [ 'My Brochure' ], 'name' => [ 'My Name ' ], 'img' => [ 'My image' ] } ] } ] };

        You don't account for the top level hash or any of the arrays.

        print $data->{record}[0]{namepairs}[0]{name}[0];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827906]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-05 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls