Perl and XML

Guys very newbie question here.

If Im using perl eg.to do various system admin tasks, let's say the following...

-------------------------------------------------- script.pl

#!/usr/bin/perl -w
use strict;
use warnings;

open(SUCCESS, ">successpcs.txt") or die "Can not create success: $!\n";
open(FAILED, ">failedpcs.txt") or die "Can not create failure: $!\n";

open(LIST,"<systems.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      system("net use s: \\$server\ipc\$");
      if(!-e 's:\status.txt') {
            print "$server doesn't have a status.txt file\n";
            next;
      }
      unless(open(IN,'s:/status.txt')){
            print "Could not open status.txt on $server\n";
            next;
      }
      local $/;
      my $data=<IN>;
      my $found=0;
      if($data =~ /success/) {
            print SUCCESS "$server\n";
            $found=1;
      }
      if($data =~ /failed/) {
            print FAILED "$server\n";
            $found=1;
      }
      next if $found;
      print "$server is not success or failed\n";
}

close(LIST);
close(SUCCESS);
close(FAILED);

----------------------------------------------------------------

How would you convert the output into xml format?

Any help greatly appreciated.
LVL 1
Simon336697Asked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
There are many ways you could put the data in an XML file.  Exactly how you want it organized will depend on how it needs to be used.   Here is one way.


#!/usr/bin/perl -w
use strict;
use warnings;

open(OUT, ">output.xml") or die "Can not create xml: $!\n";
print OUT "<systems>\n";
open(LIST,"<systems.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      system("net use s: \\$server\ipc\$");
      if(!-e 's:\status.txt') {
            print OUT "  <system name=\"$server\" status=\"NoStatus\"></system>\n";
            next;
      }
      unless(open(IN,'s:/status.txt')){
            print OUT "  <system name=\"$server\" status=\"Error\">$!</system>\n";
            next;
      }
      local $/;
      my $data=<IN>;
      my $found=0;
      if($data =~ /success/) {
            print OUT "  <system name=\"$server\" status=\"Success\"></system>\n";
            $found=1;
      }
      if($data =~ /failed/) {
            print OUT "  <system name=\"$server\" status=\"Failed\"></system>\n";
            $found=1;
      }
      next if $found;
      print OUT "  <system name=\"$server\" status=\"Unknown\"></system>\n";
}
print OUT "</systems>\n";

close(LIST);
close(OUT);
0
 
Adam314Commented:
How do you want the output to look?  Do you want just one output file, instead of a success and failed file?
0
 
Simon336697Author Commented:
Hi Adam!

To be honest, Im not sure Adam. I just wanted to see how you guys actually do this type of conversion of perl to xml output..either way would be fine mate.
0
 
Simon336697Author Commented:
Thanks Adam!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.