Question

I'm really new to perl and have an issue where I load in XML using XML::Simple and I have the tag names as hash names. I want to get at the hash stored with the name "xsd:schema" but obviously $xsd:schema doesn't work. I've spent ages googling it and cant find how to do it.

How do I get at that hash so I can find out the key values?

Edit:

Sorry I didn't explain myself very well. I want to find out the keys and values of those keys in a hash several levels deep but the name xsd:schema is causing a syntax error:

foreach my $attributes (keys %{ $data{$xsd:schema}{$xsd:element}}){
    print "$attributes : ${$data}{$xsd:schema}{$xsd:element}{$attributes}\n";
}

Edit 2: Here is how I did it.

$schemaData = $data->{'xsd:schema'}->{'xsd:element'}->{'xsd:complexType'}->{'xsd:sequence'}->{'xsd:element'}->{'xsd:complexType'}->{'xsd:sequence'}->{'xsd:element'};

print Dumper($schemaData);

foreach my $fieldName (keys %{ $schemaData}){

    $fieldType =  $schemaData->{$fieldName}->{'type'};
    print "$fieldType\n";
}
Was it helpful?

Solution

Use Data::Dumper to visualize complex data structures.

XML::Simple does not create new variables, it creates a reference. See Perl Data Structures Cookbook.

use Data::Dumper;
use XML::Simple;

my $x = XMLin(q(<r xmlns:xsd="xsd"><xsd:schema atr="a">a</xsd:schema></r>));
print Dumper $x;
print $x->{'xsd:schema'}{content};

Output:

$VAR1 = {
          'xsd:schema' => {
                          'content' => 'a',
                          'atr' => 'a'
                        },
          'xmlns:xsd' => 'xsd'
        };
a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top