2015-10-01 3 views
1

Я новичок в xml. У меня есть 10 файлов XML, которые содержат некоторые «хлюпать доклад» следующим образом (я не искал ответ только руководство о том, как я мог подойти к нему):Написание файла xml с использованием Perl

<SquishReport> 
<test name="mat_tst_versions"> 
<prolog time="2015-06-16T13:49:54+02:00"/> 
<message type="FATAL" time="2015-06-16T13:50:54+02:00"> 
    <description><![CDATA[Execution error]]></description> 
    <description type="DETAILED"><![CDATA[squishrunner exited with code 3 when executing tst_start_app]]></description> 
</message> 
<test name="tst_check_versions"> 
    <prolog time="2015-06-16T13:50:55+02:00"/> 
    <message line="274" type="LOG" file="C:\test\scripts\utilities.py" time="2015-06-16T13:50:59+02:00"> 
     <description><![CDATA[is 64 bit windows version: True]]></description> 
    </message>    
    <message line="124" type="LOG" file="C:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16T13:50:59+02:00"> 
     <description><![CDATA[ path exists!]]></description> 
    </message> 
    <message line="129" type="FAIL" file="C:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16T13:50:59+02:00"> 
     <description><![CDATA[expected version: 1.0.0.107]]></description> 
    </message> 
    <verification line="132" type="" name="" file="C:\test\mat_tst\tst_check_versions\test.py"> 
     <result type="PASS" time="2015-06-16T13:50:59+02:00"> 
      <description>version check test case passes!</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <epilog time="2015-06-16T13:50:59+02:00"/> 
</test>   
<test name="tst_scheduler_1"> 
    <prolog time="2015-06-16T13:51:00+02:00"/> 
    <message line="62" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:00+02:00"> 
     <description><![CDATA[count = 0]]></description> 
    </message> 
    <message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:51:06+02:00"> 
     <description><![CDATA[duration of start of C: 5132ms]]></description> 
    </message> 
    <message line="172" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00"> 
     <description><![CDATA[application name = C]]></description> 
    </message> 
    <message line="184" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00"> 
     <description><![CDATA[start application: C]]></description> 
    </message> 
    <message line="213" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00"> 
     <description><![CDATA[Scheduler Label exists]]></description> 
    </message> 
    <message line="219" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:51:19+02:00"> 
     <description><![CDATA[Scheduler Counter exists]]></description> 
    </message> 
    <verification line="368" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:01+02:00"> 
      <description>different types elements</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <verification line="369" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:01+02:00"> 
      <description>name_list= ['Scheduler', 'NCU', 'PLCSimAdv', 'simNCK', 'MCP']</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <verification line="370" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:01+02:00"> 
      <description>typ_list= ['Scheduler', u'NCU', u'PLCSimAdv']</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <message line="393" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:08+02:00"> 
     <description><![CDATA[:840evoVC.lblClockCounter_Edit = 0]]></description> 
    </message> 
    <verification line="411" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:08+02:00"> 
      <description>scheduler counter 1. read, n=0: Scheduler= 0</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <verification line="414" type="" name="" file="C:\test\mat_tst\tst_scheduler_1\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:08+02:00"> 
      <description>scheduler counter 2. read, n=0: Scheduler= 0</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <message line="421" type="LOG" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:08+02:00"> 
     <description><![CDATA[scheduler counter 1. read < counter 2. read, n=0: Scheduler= 0]]></description> 
    </message> 
    <message line="484" type="ERROR" file="C:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16T13:52:29+02:00"> 
     <description><![CDATA[Script Error]]></description> 
     <description type="DETAILED"><![CDATA[LookupError: Object ':840evoVC.chkSchedule_CheckBox_2' not ready.]]></description> 
    </message> 
    <epilog time="2015-06-16T13:52:29+02:00"/> 
</test> 
<test name="tst_run_plc"> 
    <prolog time="2015-06-16T13:52:30+02:00"/> 
    <verification line="45" type="" name="" file="C:\test\mat_tst\tst_run_plc\test.py"> 
     <result type="FAIL" time="2015-06-16T13:52:30+02:00"> 
      <description>user path folder already exists!</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:34+02:00"> 
     <description><![CDATA[duration of start of 840evoVC: 4263ms]]></description> 
    </message> 
    <message line="54" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:34+02:00"> 
     <description><![CDATA[start ok, duration = 4263000]]></description> 
    </message> 
    <message line="64" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:34+02:00"> 
     <description><![CDATA[application started: 840evoVC]]></description> 
    </message> 
    <message line="78" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:39+02:00"> 
     <description><![CDATA[PLC Sim Advanced tmp user files are the expected files]]></description> 
    </message> 
    <message line="263" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:45+02:00"> 
     <description><![CDATA[all applications are closed]]></description> 
    </message> 
    <message line="87" type="LOG" file="C:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16T13:52:45+02:00"> 
     <description><![CDATA[close application ok]]></description> 
    </message> 
    <epilog time="2015-06-16T13:52:45+02:00"/> 
</test> 
<test name="tst_app_resources"> 
    <prolog time="2015-06-16T13:52:47+02:00"/> 
    <message line="65" type="LOG" file="C:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16T13:52:49+02:00"> 
     <description><![CDATA[count:0]]></description> 
    </message> 
    <message line="125" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:52:53+02:00"> 
     <description><![CDATA[duration of start: 4176ms]]></description> 
    </message> 
    <message line="73" type="LOG" file="C:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16T13:52:53+02:00"> 
     <description><![CDATA[start ok, duration = 4176000]]></description> 
    </message> 
    <message line="43" type="LOG" file="C:\test\scripts\win32utilities.py" time="2015-06-16T13:53:00+02:00"> 
     <description><![CDATA[Application 840evovc only exists once]]></description> 
    </message>    
    <message line="138" type="ERROR" file="C:\test\mat_tst_\tst_app_resources\test.py" time="2015-06-16T13:53:30+02:00"> 
     <description><![CDATA[Script Error]]></description> 
     <description type="DETAILED"><![CDATA[LookupError: Object ':chkSchedule_CheckBox_2' not ready. 
      Called from: 
     C:\test\mat_tst\tst_app_resources\test.py: 82]]></description> 
    </message> 
    <epilog time="2015-06-16T13:53:30+02:00"/> 
</test> 
<test name="tst_reset"> 
    <prolog time="2015-06-16T13:53:32+02:00"/> 
    <message line="260" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:56:12+02:00"> 
     <description><![CDATA[Process exited.]]></description>   
    </message> 
    <message line="263" type="LOG" file="C:\test\scripts\start_stop.py" time="2015-06-16T13:56:12+02:00"> 
     <description><![CDATA[all applications are closed]]></description> 
    </message> 
    <message line="284" type="LOG" file="C:\test\mat_tst\tst_reset\test.py" time="2015-06-16T13:56:12+02:00"> 
     <description><![CDATA[application closed]]></description> 
    </message> 
    <verification line="287" type="" name="" file="C:\test\mat_tst\tst_reset\test.py"> 
     <result type="PASS" time="2015-06-16T13:56:12+02:00"> 
      <description>tst_reset_1 ok</description> 
      <description type="DETAILED"></description> 
     </result> 
    </verification> 
    <epilog time="2015-06-16T13:56:12+02:00"/> 
</test> 
<epilog time="2015-06-16T13:56:13+02:00"/> 
</test> 
</SquishReport> 

Теперь я хочу, чтобы создать еще один файл XML, который содержит сводку XML-файлов. Что мне нужно иметь в новом файле XML просто только </test> </SquishReport> каждый файл, а затем ссылка линии это

<result state= "Passed" href="link to the file"*.xml /> 

"Passed" если <result type="PASS" иначе "NOT Passed".

В общем, что я ожидаю в новом файле XML является:

<?xml version="1.0" encoding="utf-8"?> 
<summary> 
    <testcase name="name of testcase1"> 
    <result state= "Passed" href="link to file1"*.xml /> (for the first file) 
    <result state= "Passed" href="link to file2"*.xml /> (for the second file) 
    ...... 
    <testcase/> 
    <testcase name="name of testcase2"> 
    <result state= "Passed" href="link to file1"*.xml /> (for the first file) 
    <result state= "Passed" href="link to file2"*.xml /> (for the second file) 
    ...... 
    <testcase/> 
    .... 
    <testcase name="name of testcasen"> 
    <result state= "Passed" href="link to file1"*.xml /> (for the first file) 
    <result state= "Passed" href="link to file2"*.xml /> (for the second file) 
    ...... 
    <testcase/> 
<summary/> 

Edit: Вот то, что я до сих пор:

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; 
+1

[XML :: LibXML] (http://search.cpan.org/~shlomif/XML-LibXML-2.0122/LibXML.pod) –

+0

В чем проблема/вывод? – Pradeep

+0

Вы создаете больше сводных файлов, чем исходные файлы. Зачем? – reinierpost

ответ

2

я бы подойти к нему так:

  • Использование XML::Twig (потому что мне это нравится).
  • Создать «родительскую» XML документу
  • итерация каждый файл XML и использовать cut и paste методы в XML::Twig для передачи содержимого через (и подрезать его в случае необходимости). Я уверен, что 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, но я думаю, вы хотите больше этого. Помните - вы можете либо findnodes, либо использовать children, чтобы действовать на ... дочерние узлы, или parent, чтобы действовать на родительский узел. (например, $result -> parent -> att('file'))
  • изменить элементы.
  • вставьте их в исходный документ.

Я не мог понять, как ваши testcases/имена отображаются от источника к резюме - так оно и может быть полезно знать, что вы можете добавить в новый узел, если вы хотите:

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 в новый документ.

0

Вы создаете документ в файл на тест , Вам необходимо поднять создание документа (а также создание корня и т. Д.) Из цикла.

XPATH/SquishReport/тест/тест выглядит так, как будто это должно быть/SquishReport/тест

И в какой-то момент вам нужно будет сделать что-то с $ документ (например, для записи в файл)

+0

Спасибо. О, я забыл написать и здесь. Я сделал это сейчас. – Royeh

Смежные вопросы