Skeeve has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple XMLout tag sorting
by three18ti (Monk) on Jan 10, 2014 at 17:59 UTC |