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

Dani AI

Generated

As ’s sample shows, the input is simple hyphen-separated name–IP lines grouped by blank lines, but there’s no single “right” XML — you must pick a schema first (as pointed out). A pragmatic, robust choice is to treat each blank-line block as a <network> (or <site>) and represent devices with generic elements and attributes. That avoids creating element names from arbitrary input and makes validation and downstream processing easier than producing many ad-hoc tags.

Example XML structure (one network shown):

<networks>
  <network id="1">
    <host name="client1" role="client" address="10.1.1.1" prefix="24"/>
    <host name="clientgw1" role="gateway" address="10.1.1.2"/>
    <host name="server1" role="server" address="10.1.1.3" prefix="24"/>
    <host name="servergw1" role="gateway" address="10.1.1.4"/>
  </network>
</networks>

Practical steps to implement in Perl (no code pasted here): read the whole file and split on blank lines to get blocks; for each non-empty line split at the first hyphen into key and value; if value contains a slash, split into address and prefix; sanitize the key before using it as an element name (replace spaces, avoid leading digits, avoid colons) — or better, use a generic <host name="..."> so sanitization is minimal. Use a CPAN XML writer/parser (XML::Writer, XML::LibXML or XML::Twig) to produce well-formed XML and to handle escaping/pretty printing. Log and skip malformed lines (missing hyphen, bad IP format).

A few cautions and gaps in the thread: ’s quick example demonstrates the basic mapping but is flattened and doesn’t address grouping, escaping, or tag-name safety. Also the third block in the original sample contains clientg3w (likely a typo for clientgw3) — normalize names or preserve the raw token as an attribute for traceability. Finally, avoid hand-concatenating XML strings; use an XML library, validate with a tool like xmllint, and decide up front whether IP prefixes should be an attribute or kept in element content.

Recommended Answers

All 3 Replies

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.

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>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.