I have the following XML
<?xml version="1.0" encoding="UTF-8"?>
<Objects >
<Item1 elemId="id1" name="view" sort_id="3">
</Item1>
<Item2 elemId="id3" name="view" sort_id="4" >
</Item2>
<Item3 elemId="id5" name="view" sort_id="2">
</Item3>
<Item4 elemId="id9" name="view" sort_id="1">
</Item4>
</Objects>
I want to sort this data by the attribute sort_id to get the following:
<?xml version="1.0" encoding="UTF-8"?>
<Objects >
<Item4 elemId="id9" name="view" sort_id="1">
</Item4>
<Item3 elemId="id5" name="view" sort_id="2">
</Item3>
<Item1 elemId="id1" name="view" sort_id="3">
</Item1>
<Item2 elemId="id3" name="view" sort_id="4" >
</Item2>
</Objects>
I know that I can't do this in XML::Simple, but I heard that I can sort with XML::LibXML. I couldn't find the solution.
There is nothing built into
XML::LibXMLthat specifically provides for sorting elements, but it is simple to do using the available APISomething like this will do what you want. Unfortunately
XML::LibXMLisn't very good at producing neat output, although it is perfectly well-formed and valid. If you want something prettier then you should look atXML::LibXML::PrettyPrint, which will do this for yououtput
Update
To use
XML::LibXML::PrettyPrint, you need to addat the top of the program, and replace
with
output