in reply to XML::Simple How to print data in its original position

Hello, Z_S, and welcome to the Monastery!

Following the hints from daxim (since deleted) and tobyink (although I can’t find a solution that sets just the <nonterminal> KeyAttr option to the empty list), I came up with the following:

#! perl use strict; use warnings; use XML::Simple; my $xml = do { local $/; <DATA> }; my $xmlObj = XML::Simple->new(); my $data = $xmlObj->XMLin($xml, KeyAttr => []); my (@a_out, @ac_out); foreach my $rule ( @{ $data->{rules}{rule} } ) { if ($rule->{name} eq 'A') { push @a_out, $_->{name} for @{ $rule->{nonterminal} }; } elsif ($rule->{name} eq 'AC') { push @ac_out, $_->{name} for @{ $rule->{nonterminal} }; push @ac_out, $_ for @{ $rule->{token} }; } } print join(' ', @a_out ), "\n"; print join(' ', @ac_out), "\n"; __DATA__ <detail> <start> <nonterminal name="A" /> </start> <rules> <rule name="A" type="text"> <nonterminal name="AA" /> <nonterminal name="AC" /> <nonterminal name="AD" /> <nonterminal name="AB" /> <nonterminal name="AE" /> </rule> <rule name="AA" type="numeric"> <token>1</token> <token>10</token> </rule> <rule name="AC" type="alpanumeric"> <token>3</token> <nonterminal name="BE" /> <nonterminal name="CE" /> <token>30</token> </rule> <rule name="CE" type="special"> <token>var1</token> <token>var2</token> </rule> <rule name="BE" type="symbol"> <token>%</token> <token>#</token> </rule> </rules> </detail>

Output:

AA AC AD AB AE BE CE 3 30

I can’t see any way of retaining the interleaved ordering of rule AC’s <nonterminal> and <token> tags, though.

HTH,

Athanasius <°(((><contra mundum