The following script
#!/usr/bin/perl -- use strict; use warnings; use XML::Simple; use Data::Dumper; my $file = "test.xml"; my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($file, keyattr=>['CLASSNAME'], ForceContent=>1, +ForceArray=>1); print Dumper($doc); foreach my $element (keys %{$doc->{INNERSUB}->{INNERSUB1}}) { print "$element\n"; print "\tType:\t$doc->{INNERSUB}->{INNERSUB1}->{$element}[0]-> +{Type}\n"; print "\tContent:\t$doc->{INNERSUB}->{INNERSUB1}->{$element}[0 +]->{content}\n"; } exit(0);
With test.xml containing:
<SUB CLASSNAME="SUB1"> <INNERSUB CLASSNAME="INNERSUB1"> <Element1 Type="Type1">Content</element1> <Element2 Type="Type2">Content</element2> </INNERSUB> </SUB>
Produces
$VAR1 = { 'INNERSUB' => { 'INNERSUB1' => { 'Element2' => [ { 'Type' => 'Type +2', 'content' => 'C +ontent' } ], 'Element1' => [ { 'Type' => 'Type +1', 'content' => 'C +ontent' } ] } }, 'CLASSNAME' => 'SUB1' }; Element2 Type: Type2 Content: Content Element1 Type: Type1 Content: Content
Update: If the syntax for accessing nested data structures is not familiar, you might have a look at perlreftut.
In reply to Re: Newbie XML Simple reference
by ig
in thread Newbie XML Simple reference
by Sporti69
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |