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


In reply to Re: XML::Simple How to print data in its original position by Athanasius
in thread XML::Simple How to print data in its original position by Z_S

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.