Link to home
Start Free TrialLog in
Avatar of MKrauss
MKrauss

asked on

Sorting XML Attributes and Elements in Perl

I need to use a xml configuration file where it's most important to get the data collected in order,
the xml file looks like:

<?xml version="1.0"?>
<ROOT>
  <Def_Policy>DROP</Def_Policy>
  <Def_Parameters>true</Def_Parameters>
  <!-- Comment
    Any freetext
  -->
  <POLICY_A Name="Any Name" Enabled="1">
   <ADVICE>any advisory</ADVICE>
   <ADVICE>any advisory</ADVICE>
  </POLICY_A>
  <POLICY_B Name="Any Name" Enabled="1">
   <ADVICE>any advisory</ADVICE>
   <ADVICE>any advisory</ADVICE>
   <SUB Enabled="1">
    <SUB_ADVICE_A>OUTPUT</SUB_ADVICE_A>
    <SUB_ADVICE_A>OUTPUT</SUB_ADVICE_A>
   </SUB>
  </POLICY_B>
  <POLICY_A Name="Any other Name" Enabled="1">
   <ADVICE>any other advisory</ADVICE>
   <ADVICE>any other advisory</ADVICE>
 </POLICY_A>
</ROOT>

How can i read  this xml file into a structured hash in order ?
Usualy xml::simple is prefered but will not make it because it is somehow not made for ordering
any elements.

thx.
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
While this isn't the conventional way, I prefer to reorder my XML file before processing it. That way, I can also view it the way I want to process it.

To do that use XSLT to transform the XML, then you can process the XML from there or save it to file then do what you want with it. To use XSLT with Perl, you can look into XML::LibXSLT and XML::LibXML. There may be other libraries as well, those two are the ones I've used for XSLT transformation.