perl - Sorting an XML element's children by node name -


hi new perl , xml trying sort xml file nodes. using libxml here example of xml file

<root>         <st_5>val5</st_5>         <st_1>val1</st_1>         <st_6>val6</st_6>         <st_8>val8</st_8>         <st_4>val4</st_4>         <st_0>val0</st_0> </root> 

here code have far. of code here got form questions posted here on stackoverflow

#!/usr/bin/perl use warnings; use strict; use xml::libxml; @newnodes = qw(); $parser = xml::libxml->new(); $xmldoc = $parser->parse_file('test2_new.xml'); my($book)  = $xmldoc->findnodes ('/root/.'); $book->appendtextchild('st_2', 'stss');  $xmldoc->tofile ("test2_new.xml",2); $node ($xmldoc->findnodes('/root/*[text()]'))  {   @nodes =  $node->nodename();   push (@newnodes,@nodes);  } @x = sort { substr($a, 3) <=> substr($b, 3)  } @newnodes; print "soted list \n @x \n","\n"; sub bubble  {    unshift @{$_[0]}, $_[1]; $i( 0..@{$_[0]}-2 )  {   last if $_[0]->[$i] le $_[0]->[$i+1];   ( $_[0]->[$i],$_[0]->[$i+1] ) = ( $_[0]->[$i+1],$_[0]->[$i] )  } } 

my goal sort list add new node in xml file after inserted @ end of parent list sort nodes in xml file. every after new node added sorts nodes. able sort list on print not in xml file.

please help

using xml::xsh2, wrapper around xml::libxml:

open file.xml ; xmove &{ sort :k name() /root/* } /root ; save :b ; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -