?
Solved

Perl and XML

Posted on 2007-07-26
4
Medium Priority
?
337 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:Simon336697
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 19574790
How do you want the output to look?  Do you want just one output file, instead of a success and failed file?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19575478
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
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 19575893
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
 
LVL 1

Author Comment

by:Simon336697
ID: 19575932
Thanks Adam!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question