Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Dereferencing a hashref from XML::Simple

by helgi (Hermit)
on Jul 28, 2006 at 10:40 UTC ( #564315=perlquestion: print w/ replies, xml ) Need Help??
helgi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, fellow Monks.

I have been trying to use XML::Simple to parse and count elements from an XML file.

I am having trouble getting dereferencing the hashref structure that I get from the XMLin function. I have tried many many methods and I have read the documentation over and over again, but still no luck. Whatever I try, everything returns either empty or the reference itself.

What I want to get out is a hash %file_url, with filenames as the elements, something like

$file_url{file1.exe} = 'http://server.domain/file1.exe'; $file_url{file2.zip} = 'http://server.domain/file2.zip';

Here is the code with a much simplified version of the XML. The Data::Dumper parts are just there so that I can be sure that I am feeding the data in correctly. They are not necessary or desirable for the final code.

use warnings; use strict; ; use XML::Simple qw(:strict); use Data::Dumper; my $xml =q(<?xml version="1.0"?> <response> <files> <file file_url="http://server.domain/file1.exe"> file1.exe </file> <file file_url="http://server.domain/file2.zip"> file2.zip </file> </files> </response> ); my $ref = XMLin($xml, forcearray => [ qw(files) ], keyattr => [] ); print Dumper($ref);

Is there perhaps a better way to do this than to use XML::Simple?

I hope someone is able to help. I know this should be a really simple problem, but I'm just not getting it.


--
Regards,
Helgi Briem
hbriem AT f-prot DOT com

Comment on Dereferencing a hashref from XML::Simple
Select or Download Code
Re: Dereferencing a hashref from XML::Simple
by reneeb (Chaplain) on Jul 28, 2006 at 10:54 UTC
    Maybe this could help:
    #!/usr/bin/perl use warnings; use strict; ; use XML::Simple qw(:strict); use Data::Dumper; my $xml =q(<?xml version="1.0"?> <response> <files> <file file_url="http://server.domain/file1.exe"> file1.exe </file> <file file_url="http://server.domain/file2.zip"> file2.zip </file> </files> </response> ); my $ref = XMLin($xml, forcearray => [ qw(files) ], keyattr => [] ); my %files; for my $elem(@{$ref->{files}}){ my $filesref = $elem->{file}; $files{$_->{content}} = $_->{file_url} for(@$filesref); } print Dumper(\%files);
      Thanks reneeb,

      but when I try to run that, I get:

      not well-formed (invalid token) at line 2, column 49, byte 93 at c:/Pe +rl/site/lib/XML/Parser.pm line 185
      Any ideas?


      --
      Regards,
      Helgi Briem
      hbriem AT f-prot DOT com
Re: Dereferencing a hashref from XML::Simple
by GrandFather (Cardinal) on Jul 28, 2006 at 11:03 UTC

    I'd suggest you use XML::TreeBuilder:

    use warnings; use strict; use XML::TreeBuilder; my $xml =q(<?xml version="1.0"?> <response> <files> <file file_url="http://server.domain/file1.exe"> file1.exe </file> <file file_url="http://server.domain/file2.zip"> file2.zip </file> </files> </response> ); my $tree = XML::TreeBuilder->new (); $tree->parse ($xml); my @fileElts = $tree->find ('file'); my %file_url; $file_url{$_->attr ('file_url')} = $_->as_text () for @fileElts; print "$_: $file_url{$_}\n" for keys %file_url;

    Prints:

    http://server.domain/file2.zip: file2.zip http://server.domain/file1.exe: file1.exe

    DWIM is Perl's answer to Gödel
      Thanks, Grandfather.

      It's strange, but your code returns almost exactly the same error as the XML::Simple code above:

      not well-formed (invalid token) at line 4, column 49, byte 93 at /usr/ +lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi/XML/Parser.pm lin +e 187
      This happens on Windows (Activeperl) and two different Linux systems. Any idea what's going on?


      --
      Regards,
      Helgi Briem
      hbriem AT f-prot DOT com

        It works fine here as you can see from the sample output. I'd guess a bad character in the XML string. Try overwriting it with your original sample data or at least delete all the line ends in your editor and replace them with new ones.


        DWIM is Perl's answer to Gödel
      Sorry, I seem to have made a mistake. Your code works very well. Thank you very much for the help, Grandfather.


      --
      Regards,
      Helgi Briem
      hbriem AT f-prot DOT com
Re: Dereferencing a hashref from XML::Simple
by planetscape (Canon) on Jul 28, 2006 at 11:12 UTC

    IMHO, one of the best discussions of complex data structures, and how to access their innards, is to be found in Chapter 3. References and complex data structures of perlinter.pdf, available from Perl Training Australia. You'd probably want the most current version of the course notes, though, which have been reworked into progperl.pdf, available from the same page noted above.

    You might also wish to take a look at:


    intro to references
    References quick reference

    HTH,

    planetscape
      Thank you muchly. I will look into that. I haven't really used or studied complex data structures much up until now as I haven't needed them. That's en error I have to fix.


      --
      Regards,
      Helgi Briem
      hbriem AT f-prot DOT com

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-10-02 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls