Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

XML::Simple XMLout tag sorting

by Skeeve (Vicar)
on Jan 07, 2014 at 23:56 UTC ( #1069713=perlquestion: print w/ replies, xml ) Need Help??
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!


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

Comment on XML::Simple XMLout tag sorting
Select or Download Code
Replies are listed 'Best First'.
Re: XML::Simple XMLout tag sorting
by three18ti (Scribe) on Jan 10, 2014 at 17:59 UTC

    From personal Experience, XML::Simple is a collection of bugs flying in loose formation. I would recommend using another library such as XML::LibXML or my preference XML::Toolkit.

    It's been a while so I don't remember the specifics, but I was attempting to use XML::Simple and was getting some really strange results when parsing my xml. Strange data structures being returned... etc. It was actually #perl-help on irc.perl.org that turned me onto XML::Toolkit; after the initial learning curve I'm glad I made the change.

    anyway, that's my $0.02

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069713]
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-08-01 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls