Assuming that your input XML isn't much more complicated than the above fragment (and even if it is, this can still work), a simple XML::Parser will do the trick.

#!/usr/bin/perl -w use strict; use XML::Parser; my $parser = new XML::Parser( Style => "::localParser" ); print $parser->parsefile( $ARGV[0] ); ######################################################### ######################################################### parser ######################################################### package localParser; use strict; # # called when the parser starts # sub Init { my $self = shift(); $self->{people} = []; $self->{officers} = []; $self->{text} = ''; } sub Start { my ($self, $element, %attr) = @_; $self->{text} = ''; # clear text to be sure } sub Char { my ($self, $string) = @_; $self->{text} .= $string; # append string to text } sub End { my ($self, $element) = @_; # save string in proper # category. if you have # more complicated data # (i.e. a simple array # won't do), you'll have + # to do something more + # clever if ( $element eq 'Ch_Chair' ) { push( @{$self->{officers}}, $self->{text} ); } elsif ( $element eq 'CommitteeList' ) { push( @{$self->{people}}, $self->{text} ); } $self->{text} = ''; } # # the final output # sub Final { my $self = shift(); my $officers = join("\n\t\t\t", map{ "<person>$_</person>" } @{$self->{officers}}); my $people = join("\n\t\t\t", map{ "<person>$_</person>" } @{$self->{people}}); return <<__HERE__ <doc> <perslist> <officers> $officers </officers> $people </perslist> </doc> __HERE__ } 1;

So essentially what I'm doing is parsing the data into some intermediate data structure and then outputting that as XML. I'm sure that there are other modules on CPAN that'll help you output valid XML based on some more complicated data structure, rather than this simple collection of two arrays :)

I'm not sure if this qualifies as a "pure xml solution" though!


In reply to Re: Ugly XML processing looking for a pure XML solution by eg
in thread Ugly XML processing looking for a pure XML solution by mirod

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.