in reply to XML:Simple Config

If you look at the output of the Dumper, it will tell you how to access the complex structure. Anything that is in curly brackets is a hash and anything in square brackets is an array.

I modified your code as follows:

use strict; use XML::Simple; use Data::Dumper; my $config = XMLin('config.xml'); print Dumper($config); print "$config->{Application}->{Name}\n"; print "$config->{Application}->{Location}->{Name}\n"; print "$config->{Application}->{Location}->{ZDrive}\n"; print "$config->{Application}->{Location}->{CDrive}\n";

In your sample XML file, there's basically 4 pieces of information stored. The last 4 print statements accesses those pieces. Here's the output.

$VAR1 = { 'Application' => { 'Location' => { 'ZDrive' => 'Z:\\Temp', 'CDrive' => 'C:\\Temp', 'Name' => 'Loc1' }, 'Name' => 'App1' } }; App1 Loc1 Z:\Temp C:\Temp

By looking at my modified code and its output, that should help you understand what I'm talking about with using the output from Dumper to decipher the syntax needed to access the complex data structure.

Replies are listed 'Best First'.
Re^2: XML:Simple Config
by PerlScholar (Acolyte) on Sep 02, 2010 at 13:19 UTC
    Exactly what I was looking for. Thanks!

      If you are going to stick with XML::Simple, then you should probably read MJD's tutorial on references and also take a look at this article on avoiding common bugs with using XML::Simple.

      I originally created XML::Simple specifically for the purpose of reading XML config files but I almost never use them any more. It wasn't obvious to me just how bad an idea it was until I did some work with Java :-)