On Wed, Oct 20, 2010 at 7:55 AM, Jyoti <jcutiep...@gmail.com> wrote:
> Now i want all the similar tags to be one below the other like :
>
>  <message name>
> </message name>
>
>  <message name>
> </message name>
>
>  <portType>
> </portType>
>
>  <portType>
> </portType>
>
>  <binding>
> </binding>
>
>  <binding>
> </binding>
>

As Rob Dixon said, if this is actually XML then you're going to want
to use a library to achieve this. Manually parsing XML is complicated
(parsing HTML or other loose SGML variants is even more complicated
due to the looser rules). Obviously, what you've given us isn't
well-formed XML because of the 'name' part of the 'message' tag.
However, if it were processing well-formed XML then you could do
something like this:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;
use XML::Simple;

$Data::Dumper::Sortkeys = 1;

if(@ARGV != 1)
{
    print STDERR "Usage: sort-xml.pl infile\n";
    exit 1;
}

my $input_filename = $ARGV[0];

my $data = XMLin($input_filename, KeepRoot => 1);

// There may be better ways to accomplish this.
// eval is normally considered dangerous in most languages.
{
    my $VAR1;

    $data = eval Dumper $data or die("Failed to evaluate data: $@");
}

my $xml = XMLout($data, KeepRoot => 1);

print $xml;

__END__

To see it in action:

[bamcc...@krypton example]$ ls -la
total 16
drwxrwxr-x.  2 bamccaig bamccaig 4096 Oct 20 14:13 .
drwxrwxr-x. 52 bamccaig bamccaig 4096 Oct 20 13:55 ..
-rw-rw-r--.  1 bamccaig bamccaig  276 Oct 20 14:11 sample.xml
-rw-rw-r--.  1 bamccaig bamccaig  420 Oct 20 14:13 sort-xml.pl
[bamcc...@krypton example]$ cat sample.xml
<root>
    <message_name></message_name>
    <message_name></message_name>
    <binding></binding>
    <message_name></message_name>
    <portType></portType>
    <binding></binding>
    <message_name></message_name>
    <portType></portType>
    <binding></binding>
</root>

[bamcc...@krypton example]$ perl sort-xml.pl
Usage: sort-xml.pl infile
[bamcc...@krypton example]$ perl sort-xml.pl sample.xml
<root>
  <binding></binding>
  <binding></binding>
  <binding></binding>
  <message_name></message_name>
  <message_name></message_name>
  <message_name></message_name>
  <message_name></message_name>
  <portType></portType>
  <portType></portType>
</root>
[bamcc...@krypton example]$

I used XML::Simple to parse the XML and used Data::Dumper to sort the
elements. However, that may or may not be robust enough for your
usage. There are many other XML modules available that might be able
to work for you if XML::Simple can't. There are also many HTML and
SGML ones that might help. I suggest you start by searching CPAN:
http://search.cpan.org/

It might help if you could be more specific about the data format.

Note: this is the first time that I've ever tried using eval in Perl
so feel free to rip me a new one for doing it wrong. :D

-- 
Brandon McCaig <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to