http://qs321.pair.com?node_id=1077777


in reply to Re^2: Parsing XML
in thread Parsing XML

That is problem with XML::Simple :) sure there are options but its easier to use XML::Rules , its is XML::Simple on steriods, exampe
#!/usr/bin/perl -- #~ 2014-03-10-19:06:55 by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 #!/usr/bin/perl -- use strict; use warnings; use XML::Rules; use XML::Simple qw/ XMLin /; use Data::Dump qw/ dd /; my $shortxmlraw = q{<?xml version="1.0" encoding="utf-8"?> <ApiRespons +e Status="OK" xmlns="http://api.namecheap.com/xml.response"> <Command +Response Type="namecheap.domains.getList"> <DomainGetListResult> <Dom +ain Expires="03/31/2015" ID="8888888" Name="Domain1.com"/> </DomainGe +tListResult> </CommandResponse> </ApiResponse>}; my $xmlraw = q{<?xml version="1.0" encoding="utf-8"?> <ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response" +> <Errors /> <Warnings /> <RequestedCommand>namecheap.domains.getList</RequestedCommand> <CommandResponse Type="namecheap.domains.getList"> <DomainGetListResult> <Domain ID="8888888" Name="Domain1.com" Expires="03/31/2015"/> <Domain ID="8888889" Name="Domain2.com" Expires="02/25/2015"/> <Domain ID="8888899" Name="Domain3.com" Expires="04/01/2015"/> <Domain ID="8888999" Name="Domain4.com" Expires="05/20/2015"/> </DomainGetListResult> <Paging> <TotalItems>4</TotalItems> <CurrentPage>1</CurrentPage> <PageSize>50</PageSize> </Paging> </CommandResponse> <Server>API02</Server> <GMTTimeDifference>--5:00</GMTTimeDifference> <ExecutionTime>0.008</ExecutionTime> </ApiResponse> }; my $xmlin = XML::Rules->new( qw/ stripspaces 8 /, rules => ## the result of xml2XMLRules foo.xml foo2.xml .... { 'ApiResponse,CommandResponse,DomainGetListResult,Paging' => 'n +o content', 'Domain' => 'as array no content', 'CurrentPage,Errors,ExecutionTime,GMTTimeDifference,PageSize,R +equestedCommand,Server,TotalItems,Warnings' => 'content' }, ); my $res = $xmlin->parse( $xmlraw ); dd( $res ); #~ dd( XMLin( $xmlraw ) ); Shabba( $res->{ApiResponse} ); $res = $xmlin->parse( $shortxmlraw ) ; dd( $res ); dd( XMLin( $shortxmlraw ) ); Shabba( $res->{ApiResponse} ); Shabba( XMLin( $shortxmlraw ) ); ## Not an ARRAY reference #~ dd( $xmlin->parse( q{} ) ); exit( 0 ); sub Shabba { my( $result ) = @_; #~ foreach my $domain (@{$result->{ApiResponse}{CommandResponse}{D +omainGetListResult}{Domain}}) { foreach my $domain (@{$result->{CommandResponse}{DomainGetListResu +lt}{Domain}}) { printf "%s %s %s\n", $domain->{Name}, $domain->{ID}, $domain->{Expires}, ;;;;;; } } __END__ { ApiResponse => { CommandResponse => { DomainGetListResult => { Domain => [ { Expires => "03/31/2015", ID => 8888888, Name => "Domain1.c +om" }, { Expires => "02/25/2015", ID => 8888889, Name => "Domain2.c +om" }, { Expires => "04/01/2015", ID => 8888899, Name => "Domain3.c +om" }, { Expires => "05/20/2015", ID => 8888999, Name => "Domain4.c +om" }, ], }, Paging => { CurrentPage => 1, PageSize => 50, TotalItems => 4 }, Type => "namecheap.domains.getList", }, Errors => undef, ExecutionTime => 0.008, GMTTimeDifference => "--5:00", RequestedCommand => "namecheap.domains.getList", Server => "API02", Status => "OK", Warnings => undef, xmlns => "http://api.namecheap.com/xml.response", }, } Domain1.com 8888888 03/31/2015 Domain2.com 8888889 02/25/2015 Domain3.com 8888899 04/01/2015 Domain4.com 8888999 05/20/2015 { ApiResponse => { CommandResponse => { DomainGetListResult => { Domain => [ { Expires => "03/31/2015", ID => 8888888, Name => "Domain1.c +om" }, ], }, Type => "namecheap.domains.getList", }, Status => "OK", xmlns => "http://api.namecheap.com/xml.response", }, } { CommandResponse => { DomainGetListResult => { Domain => { Expires => "03/31/2015", ID => 8888888, Name => "Dom +ain1.com" }, }, Type => "namecheap.domains.getList", }, Status => "OK", xmlns => "http://api.namecheap.com/xml.response", } Domain1.com 8888888 03/31/2015 Not an ARRAY reference at xml-simple-to-xml-rules-1077492.pl line 63.