Dear fellow monks,

I'm using XML::Simple to create some XML files. As the sequence of my tags is important, I was wondering whether it's possible to define the sort order of them. I found this post on stackoverflow: http://stackoverflow.com/questions/1400850/how-can-i-order-tags-in-xmlsimples-output/ which I found quite helpful.

But then I thought that this can be done better and so I created a new constructor for my class which accepts a new configuration option. This way I can define my sortorder when constructing my XML::Simple object.

Now my question is: Could you please check whether or not this can be improved? I'm not so used to this object oriented package stuff. So maybe there is a better way how I can achieve the same result?

For example: I first tried to add my 'SortOrder' configuration option to the @KnownOptOut array of XML::Simple, but couldn't find a way to do that. If that would be possible, no additional constructor would be required. But as I said: I'm not so used to this OO stuff.

Update on the previous paragraph: In the meantime I think, injecting my option into XML::Simple's option array is a bad idea, as it will also affect other objects using XML::Simple.

But here now is my code:

use strict; use warnings; # creating my object with a sortorder my $xs = new MyXMLSimple( XMLDecl => '<?xml version="1.0" encoding="UTF-8" standalone="yes"? +>', KeepRoot => 1, KeyAttr => [ ], SortOrder => { supertag => [qw[ tag1 tag3 tag4 tag10 ]], }, ); # Some testdata my $structure = { supertag => { tag10 => { content => 'value 2', }, tag3 => { content => 'value 3', }, tag1 => { content => 'value 1', }, tag91 => { content => 'value 91', }, tag90 => { content => 'value 90', }, tag4 => { content => 'value 4', }, } }; print $xs->XMLout($structure); ###################################################################### +########## package MyXMLSimple; # my XML::Simple subclass use base 'XML::Simple'; use Carp; sub new { my $class= shift; if(@_ % 2) { croak "Default options must be name=>value pairs (odd number s +upplied)"; } my %raw_opt = @_; # removing my new option from the options supplied my $sortorder= delete $raw_opt{'SortOrder'}; # creating the XML::Simple element my $self= $class->SUPER::new( %raw_opt ); # adding the sortorder in $self->{'SortOrder'}= $sortorder; return (bless($self, $class)); } # Overriding the method here sub sorted_keys { my ($self, $name, $hashref) = @_; # check whether we have a sortorder for $name my $ordered= $self->{'SortOrder'}->{$name}; if ($ordered) { # if so, prepare the sortorder my %ordered_hash; @ordered_hash{@$ordered} = (); #set ordered tags in front of others return grep {exists $hashref->{$_}} @$ordered, grep {not exists $ordered_hash{$_}} $self->SUPER::sorted_keys( +$name, $hashref); } return $self->SUPER::sorted_keys($name, $hashref); # for the rest, +I don't care! }

Many thanks in advance for any useful hints!


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

In reply to XML::Simple XMLout tag sorting by Skeeve

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.