从多个打印不同xml输出的perl脚本打印一个xml输出

时间:2012-06-19 15:32:30

标签: xml perl

我之前已经问过这个问题,但没有得到满意的答案,因为我没有提供适当的例子来看待。

我有多个脚本使用xsl来打印这些脚本的xml输出。我想编写一个主脚本来调用这些脚本并组合输出并打印一个输出。有人可以帮帮我吗?

#Example Script 1

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use Getopt::Long;

my $output = '';
my $debug = 0;
my $path;
GetOptions('path=s' => \$path,'output=s' => \$output, 'debug=i' => \$d
+ebug);

if($output eq ''){
    die ("parameter --output=s is missing");
}     
open my $xmloutput, ">", $outputFile or die "can not open $outputFile 
+";
 print $xmloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-s
+tylesheet type=\"text/xsl\" href=\"book.xsl\"?>\n<Books>\n";

my $parser = new XML::Simple;
my $data = $parser->XMLin("$path");
print $xmloutput " <bookDetails> \n";
print $xmloutput "  <bookName>$data</bookName> \n";
print $xmloutput " </bookDetails> \n";
print $xmloutput " </Books> \n";
close $xmloutput;

示例2

EXAMPLE 2
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use Getopt::Long;

my $output = '';
my $debug = 0;
my $path;
GetOptions('path=s' => \$path,'output=s' => \$output, 'debug=i' => \$d
+ebug);

if($output eq ''){
    die ("parameter --output=s is missing");
}     
open my $xmloutput, ">", $outputFile or die "can not open $outputFile 
+";
 print $xmloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-s
+tylesheet type=\"text/xsl\" href=\"Piano.xsl\"?>\n<Piano>\n";

my $parser = new XML::Simple;
my $data = $parser->XMLin("$path");
print $xmloutput " <PianoDetails> \n";
print $xmloutput "  <PianoName>$data</PianoName> \n";
print $xmloutput " </PianoDetails> \n";
print $xmloutput " </Piano> \n";
close $xmloutput;

这就是我在做什么。

  my $output = '';


   my $example1 = `perl script1.pl --path=c:/cygwin/home/username/directory --debug=1`;
    my $example2 = `perl script2.pl --path=c:/cygwin/home/username/directory --debug=1`;

 open my $finaloutput, ">" $output or die "cant open the file";
  print $finaloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet     
          type=\"text/xsl\" href=\"final.xsl\"?>\n<OutputDestination>\n";

   my @attachscript  = ($example1, $example2);

   for my $attached (@attachscript) {
     print $finaloutput "<outputgoes_here_from_script> $attached   
                         </outputgoes_here_from_script>
    }

     PRINT $finaloutput "</OutputDestination>"
     close $finaloutput;

有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

如果脚本1和脚本2无法转换为Perl模块并在包装器中直接调用(作为函数)(或者甚至只是重写为一个新脚本),我会考虑在包装器中执行此操作:

open (STDOUT, ">$output") or die "Can't open STDOUT: $!";
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"final.xsl\"?>\n<OutputDestination>\n";
print "<outputgoes_here_from_script>";
`perl script1.pl --path=c:/cygwin/home/username/directory --debug=1`;
print "</outputgoes_here_from_script>";
print "<outputgoes_here_from_script>";
`perl script2.pl --path=c:/cygwin/home/username/directory --debug=1`;
print "</outputgoes_here_from_script>";
print "</OutputDestination>";
close(STDOUT);

在回溯系统调用之前,您可能需要“打印”,但首先尝试这种方式。但是,它与您的方法没什么不同,只是省略了保存每个脚本输出的需要。