undef $/; open(IN,"sample_out.xml") || die "can not open the out xml file"; $str=; close IN; $headera=""; $headerb=""; $footer=""; $has=(); $headera=$& if ($str =~ /<\?xml.*\?>/si); $headerb=$& if ($str =~ /]+>/si); while ($str =~ /.*?<\/item>/gsi) { $con=$&; if ($con =~ /\s*(\d+)\s*<\/itemid>/) { $puino=$1; $has{$puino}=$con; } } print OUT $headera; print OUT $headerb; foreach $ke(sort keys %has) { print OUT $has{$ke}; } print OUT $footer; close OUT;