in reply to Re^2: Sort xml based on attribute (use a parser)
in thread Sort xml based on attribute
Thanks a lot to all. The code provided by you was working as per my requirement. I am from Java background from past 9 years, recently took up work on Perl. As I am new to Perl, I am not very familiar with RegEx.. trying to learn :) Thanks a lot for your time and help. I had tried to do using LibXML. Here is my code... Even I added sort functionality. But it is incomplete,, commented the sort part.
#!/usr/bin/perl use strict; use warnings; use strict; use XML::LibXML; my ($inputfile,$struct,$outputfile,$parser) indentXML(); sortOnPUI(); sub indentXML { $inputfile = 'OpsbankII_10_items.xml'; $parser = XML::LibXML->new(); $struct = $parser->parse_file($inputfile); #print $struct->toString(2); $outputfile="output.xml"; print $struct->toFile($outputfile,2); } sub sortOnPUI { my $rootel = $struct -> getDocumentElement(); my $rootelname = $rootel -> getName(); print "Root element is a $rootelname\n"; my @items = $rootel->getElementsByTagName('item'); foreach my $item (@items) { my @itemids = $item->getElementsByTagName('itemid'); foreach my $itemid (@itemids){ #print "itemid content is ",$itemid->textContent,"\n"; my @attrs= $itemid->attributes; foreach my $at (@attrs) { #print "itemid attribute content is ",$at->textContent,"\n"; if ($at->textContent eq "PUI") { print "Attribute is ", $at->textContent," and value is " +, $itemid->to_literal," \n"; # my @sorted_class_elems # = sort { $a->findvalue('itemid') <=> $b->findvalue( +'itemid') } # @items; }#if }#foreach }#foreach }#foreach }
|
|---|