Sorting XML Attributes and Elements in Perl

Posted on 2007-10-02
Last Modified: 2009-06-30
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"?>
  <!-- Comment
    Any freetext
  <POLICY_A Name="Any Name" Enabled="1">
   <ADVICE>any advisory</ADVICE>
   <ADVICE>any advisory</ADVICE>
  <POLICY_B Name="Any Name" Enabled="1">
   <ADVICE>any advisory</ADVICE>
   <ADVICE>any advisory</ADVICE>
   <SUB Enabled="1">
  <POLICY_A Name="Any other Name" Enabled="1">
   <ADVICE>any other advisory</ADVICE>
   <ADVICE>any other advisory</ADVICE>

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.

Question by:MKrauss
    LVL 39

    Accepted Solution

    There are several XML modules that help, it depends on how you want to access the data.
    You can take a look at CPAN:
    LVL 4

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now