Perl Maven

Do you really want to improve yourself?

Perl Maven

Thank you for registering!

How to preserve XML::Simple element order

Posted on 2007-12-12 01:10:31-08 by tirto

hi,
in the faq doc , it is mentioned that in the future Tie::IxHash could be use in XML::Simple.
i am using v2.18 at the moment, is there any hack to preserve the order of the data in the hashref?
thanks in advance,

tirto

Posted on 2007-12-12 01:36:21-08 by grantm in response to 6670

Retaining element order is not and never will be a feature of XML::Simple.
For some XML document types you might be able to hack it in by subclassing
XML::Simple and overriding the new_hashref() method to supply a hashref tied to Tie::IxHash.
That could solve the ABC case but it won't solve the ABA case.

The short answer is that if you care about element order then you should not use XML::Simple.
XML::LibXML is an excellent alternative which for many use cases is really no harder
to use than XML::Simple - as described in this article.