0

Hello Everyone

I am struck how to start script

in my .txt file i have some of ips and i need to retive the ip and it need to convet into xml file

in .txt file ips like his

client1-10.1.1.1/24
clientgw1-10.1.1.2
server1-10.1.1.3/24
servergw1-10.1.1.4

client2-10.1.2.1/24
clientgw2-10.1.2.2
server2-10.1.2.3/24
servergw2-10.1.2.4

client3-10.1.3.1/24
clientg3w-10.1.3.2
server3-10.1.3.3/24
servergw3-10.1.3.4

i need to open the file and these ip need to convert into xml fomat. please hepl in this

3
Contributors
3
Replies
26
Views
2 Years
Discussion Span
Last Post by 2teez
0

As there is no ‘generic’ XML format that one can just convert anything into, you’ll need to specify the XML schema before anyone can help you with your task.

1

Hi,
Maybe the OP wanted something like so:

use warnings;
use strict;

my $sep = qr/-/;
my $xml = qq[<opt_cisco>\n];
while (<DATA>) {
    next if /^$/;
    chomp;
    my ( $tag, $content ) = split /$sep/, $_, 2;
    $xml .= qq[<$tag>$content</$tag>\n];
}
print $xml, qq[</opt_cisco>\n];

__DATA__
client1-10.1.1.1/24
clientgw1-10.1.1.2
server1-10.1.1.3/24
servergw1-10.1.1.4

client2-10.1.2.1/24
clientgw2-10.1.2.2
server2-10.1.2.3/24
servergw2-10.1.2.4

client3-10.1.3.1/24
clientg3w-10.1.3.2
server3-10.1.3.3/24
servergw3-10.1.3.4

Which will give an output like so:

<opt_cisco>
<client1>10.1.1.1/24</client1>
<clientgw1>10.1.1.2</clientgw1>
<server1>10.1.1.3/24</server1>
<servergw1>10.1.1.4</servergw1>
<client2>10.1.2.1/24</client2>
<clientgw2>10.1.2.2</clientgw2>
<server2>10.1.2.3/24</server2>
<servergw2>10.1.2.4</servergw2>
<client3>10.1.3.1/24</client3>
<clientg3w>10.1.3.2</clientg3w>
<server3>10.1.3.3/24</server3>
<servergw3>10.1.3.4</servergw3>
</opt_cisco>
0

Need I add that you might be better using an XML module like XML::LibXML in CPAN.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.