Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Perl编写xml文件_Xml_Perl - Fatal编程技术网

使用Perl编写xml文件

使用Perl编写xml文件,xml,perl,Xml,Perl,我是xml新手。我有10个XML文件,其中包含一些“挤压报告”,如下所示(我不是在寻找答案,只是寻找如何处理它的指导): 编辑:以下是我目前掌握的信息: sub FileCount { chdir( $dir ) or die "Couldn't go inside $dir directory, $!"; opendir(my $dh, $dir) or die "$0: $dir: $!\n"; while (my $file = readdir($dh)) { # We onl

我是xml新手。我有10个XML文件,其中包含一些“挤压报告”,如下所示(我不是在寻找答案,只是寻找如何处理它的指导):

编辑:以下是我目前掌握的信息:

  sub FileCount
{
chdir( $dir ) or die "Couldn't go inside $dir directory, $!";
opendir(my $dh, $dir) or die "$0: $dir: $!\n";
while (my $file = readdir($dh)) {
    # We only want files
     next unless (-f "$dir/$file");
    # Use a regular expression to find files ending in .xml
     next unless ($file =~ m/$suff$/);

     my $xml = XML::LibXML->load_xml(location => $file);

    # Iterate the entries                           
     for my $test ($xml->findnodes('/SquishReport/test/test')) {
        my $name_test = $test->findvalue('@name');  

        # looks for if the results is OK or not
        for my $result ($test->findnodes('./verification/result')) {
            my $type_result = $result->findvalue('@type');
            ### -----------------------------------------------------------------------
             my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
             my $root = $doc->createElement("summary");
             $root->setAttribute('testcase_name'=> "$name_test");

             for my $case_name (keys %tags) {
                 my $tag = $doc->createElement($case_name);
                 my $value = $tags{$case_name};
                 $tag->appendTextNode($value);
                 $root->appendChild($tag);
             }

            $doc->setDocumentElement($root);
            ### -----------------------------------------------------------------------
            print "$type_result = $value_result \n";
            if (($type_result eq "WARNING") || ($type_result eq "FAIL") || ($type_result eq "ERROR") || ($type_result eq "FATAL")){
                $value_result = "OK";
                print "***********  $name_test is not OK *********** \n\n "; 
            }
            else{
                $value_result = "NOT_OK";
                print "***********  $name_test is OK *********** \n\n ";
            }
        }
        print "\n";
     }
    #}
     print "$file\n";       
     $count = $count + 1;
}
closedir($dh);  

my $filename = 'report.xml';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh $doc->toString();
close $fh;

您正在为每个测试的每个文件创建一个文档。您需要将文档创建(以及根创建等)从循环中提升出来

XPATH/SquishReport/test/test看起来应该是/SquishReport/test


而且,在某个时候,您将需要使用$doc执行某些操作(例如写入文件)

我将这样处理它:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $new_xml = XML::Twig->new( pretty_print => 'indented_a' );
$new_xml->set_root( XML::Twig::Elt->new('summary') );
$new_xml->set_xml_version('1.0');
$new_xml->set_encoding('utf-8');

my $input_processor = XML::Twig->new->parsefile('sample_squish.xml');
foreach my $result ( $input_processor->findnodes('//result') ) {
    $result->cut;
    $result->paste( $new_xml->root );
}

open( my $output, '>', 'summary.xml' ) or die $!;
print {$output} $new_xml->sprint;
close($output);
  • 使用
    XML::Twig
    (因为我喜欢它)
  • 创建“父”XML文档
  • 迭代每个XML文件,并使用
    XML::Twig
    中的
    cut
    paste
    方法跨文件传输内容(必要时对其进行修剪)。如果您愿意,我非常肯定
    XML::LibXML
    也可以做到这一点
  • 打印“父”文档
有点像这样:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $new_xml = XML::Twig->new( pretty_print => 'indented_a' );
$new_xml->set_root( XML::Twig::Elt->new('summary') );
$new_xml->set_xml_version('1.0');
$new_xml->set_encoding('utf-8');

my $input_processor = XML::Twig->new->parsefile('sample_squish.xml');
foreach my $result ( $input_processor->findnodes('//result') ) {
    $result->cut;
    $result->paste( $new_xml->root );
}

open( my $output, '>', 'summary.xml' ) or die $!;
print {$output} $new_xml->sprint;
close($output);
您需要:

  • 使用
    foreach
    /
    glob
  • 选择
    findnodes
    表达式。上面只收集
    result
    元素,但我认为您需要更多。记住-您可以再次
    查找节点
    ,也可以使用
    子节点
    对。。。子节点,或
    父节点
    ,对父节点进行操作。(例如,
    $result->parent->att('file')
  • 修改元素
  • 将它们粘贴到原始文档中
我不太清楚您的测试用例/名称是如何从源代码映射到摘要的——因此,如果您愿意,还可以添加一个新节点,这可能会很有用:

my $testcase = $new_xml -> root -> insert_new_elt('testcase', { name => "name_of_the_test" }); 
$testcase ->set_att('some_other_att', 42);
$result->cut;
$result->paste( $testcase );

这将把一个文件中的
$result
元素插入新文档中新创建的
testcase
元素中

问题/输出是什么?您创建的摘要文件比原始文件多。为什么?@reinierpost我只想创建一个文件,包括所有需要的数据。这些数据取自原始文件。@我没有所需的输出。这就是我没有在这里写输出的原因。谢谢。哦,我也忘了在这里写。我现在知道了。
#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $new_xml = XML::Twig->new( pretty_print => 'indented_a' );
$new_xml->set_root( XML::Twig::Elt->new('summary') );
$new_xml->set_xml_version('1.0');
$new_xml->set_encoding('utf-8');

my $input_processor = XML::Twig->new->parsefile('sample_squish.xml');
foreach my $result ( $input_processor->findnodes('//result') ) {
    $result->cut;
    $result->paste( $new_xml->root );
}

open( my $output, '>', 'summary.xml' ) or die $!;
print {$output} $new_xml->sprint;
close($output);
my $testcase = $new_xml -> root -> insert_new_elt('testcase', { name => "name_of_the_test" }); 
$testcase ->set_att('some_other_att', 42);
$result->cut;
$result->paste( $testcase );