错误:XML::LibXML->;加载:在8.pl第144行指定位置、字符串或IO
我使用第一个答案来创建一个使用Perl的XML文件 这是我的密码:错误:XML::LibXML->;加载:在8.pl第144行指定位置、字符串或IO,xml,perl,xml-libxml,Xml,Perl,Xml Libxml,我使用第一个答案来创建一个使用Perl的XML文件 这是我的密码: use strict; use warnings; use feature qw( say ); use XML::LibXML; use XML::Simple; use XML::LibXML::XPathContext qw( ); use XML::Writer; use IO::File; use File::Copy; use List::Util; ###****************************
use strict;
use warnings;
use feature qw( say );
use XML::LibXML;
use XML::Simple;
use XML::LibXML::XPathContext qw( );
use XML::Writer;
use IO::File;
use File::Copy;
use List::Util;
###****************************************************************
my $dir = "D:/XML";
my $count = 0;
my $entry;
my $case;
my $xmlFile;
my $case_entry;
my $entry_name;
my $result;
my $value_result;
my $value_result_end;
my $type_result;
my @testFile = <*.xml>;
my $arrSize = @testFile;
my @testCase;
my $testCasesize = @testCase;
my @test_result;
my $test_resultsize = @test_result;
#################################################################################
sub WriteXML {
chdir( $dir ) or die "Couldn't go inside $dir directory, $!";
opendir( my $dh, $dir ) or die "$0: $dir: $!\n";
###****************************************************************
my $output = IO::File->new( ">test.xml" );
my $writer = XML::Writer->new( OUTPUT => $output );
$writer->xmlDecl( 'utf-8' );
$writer->pi( 'xml-stylesheet', 'type="text/xsl" href="file:///D:/XML/xml2html_new.xslt"' );
$writer->startTag( "Summary" );
$writer->characters( "\n" );
$writer->startTag( "test", "name" => "Test_tst" );
$writer->characters( "\n" );
###****************************************************************
my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs( sr => 'http://www.froglogic.com/XML2' );
my $doc = XML::LibXML->load_xml( location => $testFile[0] );
for $entry ( $xpc->findnodes( '/sr:SquishReport/sr:test/sr:test', $doc ) ) {
$testCase[$count] = $entry->getAttribute( 'name' );
$count = $count + 1;
}
for $case ( @testCase ) {
$writer->startTag( "test", "name" => "$case" );
$writer->characters( "\n" );
for $xmlFile ( @testFile ) {
my $docFile = XML::LibXML->load_xml( location => $xmlFile );
my $attribute = qq(\@name="$case");
my $num = 0;
for $case_entry ( $xpc->findnodes( "//sr:SquishReport/sr:test/sr:test[$attribute]/sr:verification/sr:result", $docFile ) ) {
$type_result = $case_entry->findvalue( '@type' );
if ( ( $type_result eq "FAIL" ) || ( $type_result eq "ERROR" ) || ( $type_result eq "FATAL" ) ) {
$value_result = "NotOK";
}
elsif ( ( $type_result eq "PASS" ) ) {
$value_result = "OK";
}
elsif ( $type_result eq "WARNING" ) {
$value_result = "WARR";
}
else {
$value_result = "UNDEF";
}
$test_result[$num] = $value_result;
$num = $num + 1;
}
if ( @test_result == grep { $_ eq "OK" } @test_result ) {
$value_result_end = "OK";
}
elsif ( grep { $_ eq 'NotOK' } @test_result ) {
$value_result_end = "NotOK";
}
elsif ( grep { ( $_ eq 'WARR' ) } @test_result ) {
$value_result_end = "WARR";
}
else {
$value_result_end = "UNDEF";
}
# print "$value_result_end";
$writer->startTag( "result", "state" => "$value_result_end" );
$writer->characters( "\n" );
$writer->startTag( "description", "href" => "$xmlFile" );
$writer->characters( "\n" );
$writer->startTag( "description" );
$writer->characters( "\n" );
$writer->cdata( $xmlFile );
$writer->characters( "\n" );
$writer->endTag( "description" );
$writer->endTag( "description" );
$writer->endTag( "result" );
}
$writer->endTag( "test" );
$writer->characters( "\n" );
}
###****************************************************************
$writer->endTag( "test" );
$writer->characters( "\n" );
$writer->endTag( "Summary" );
$writer->end();
$output->close();
}
###****************************************************************
&WriteXML( $dir );
system( "summary.xml" );
使用严格;
使用警告;
使用特征qw(例如);
使用XML::LibXML;
使用XML::Simple;
使用XML::LibXML::XPathContext qw();
使用XML::Writer;
使用IO::文件;
使用文件::复制;
使用列表::Util;
###****************************************************************
my$dir=“D:/XML”;
我的$count=0;
我的$entry;
我的$case;
我的$xmlFile;
我的$case_条目;
我的$entry\u姓名;
我的$result;
我的$value\u结果;
我的$value\u结果\u结束;
我的$type_结果;
my@testFile=;
我的$arrSize=@testFile;
我的@testCase;
我的$testCasesize=@testCase;
我的@test_结果;
我的$test\u resultsize=@test\u result;
#################################################################################
子WriteXML{
chdir($dir)或die“无法进入$dir目录,$!”;
opendir(my$dh,$dir)或死“$0:$dir:$!\n”;
###****************************************************************
my$output=IO::File->new(“>test.xml”);
my$writer=XML::writer->new(输出=>$OUTPUT);
$writer->xmlDecl('utf-8');
$writer->pi('xml样式表','type=“text/xsl”href=”file:///D:/XML/xml2html_new.xslt"' );
$writer->startTag(“摘要”);
$writer->characters(“\n”);
$writer->startTag(“测试”、“名称”=>“测试”);
$writer->characters(“\n”);
###****************************************************************
我的$xpc=XML::LibXML::XPathContext->new();
$xpc->registerNs(sr=>'http://www.froglogic.com/XML2' );
my$doc=XML::LibXML->load_XML(位置=>$testFile[0]);
对于$entry($xpc->findnodes('/sr:SquishReport/sr:test/sr:test',$doc)){
$testCase[$count]=$entry->getAttribute('name');
$count=$count+1;
}
对于$case(@testCase){
$writer->startTag(“测试”,“名称”=>“$case”);
$writer->characters(“\n”);
对于$xmlFile(@testFile){
my$docFile=XML::LibXML->load_XML(位置=>$xmlFile);
我的$attribute=qq(\@name=“$case”);
我的$num=0;
对于$case_条目($xpc->findnodes(//sr:SquishReport/sr:test/sr:test[$attribute]/sr:verification/sr:result“,$docFile)){
$type_result=$case_entry->findvalue('@type');
if($type_result eq“FAIL”)| |($type_result eq“ERROR”)| |($type_result eq“FATAL”)){
$value\u result=“NotOK”;
}
elsif($type_结果等式“PASS”)){
$value\u result=“确定”;
}
elsif($type_result eq“WARNING”){
$value\u result=“WARR”;
}
否则{
$value\u result=“未定义”;
}
$test\u result[$num]=$value\u result;
$num=$num+1;
}
if(@test\u result==grep{$\ueq“OK”}@test\u result){
$value\u result\u end=“确定”;
}
elsif(grep{$\eq'NotOK'}@test\u结果){
$value\u result\u end=“NotOK”;
}
elsif(grep{($\eq'WARR')}@test\u结果){
$value\u result\u end=“WARR”;
}
否则{
$value\u result\u end=“未定义”;
}
#打印“$value\u result\u end”;
$writer->startTag(“结果”,“状态”=>“$value\u结果\u结束”);
$writer->characters(“\n”);
$writer->startTag(“说明”,“href”=>“$xmlFile”);
$writer->characters(“\n”);
$writer->startTag(“说明”);
$writer->characters(“\n”);
$writer->cdata($xmlFile);
$writer->characters(“\n”);
$writer->endTag(“说明”);
$writer->endTag(“说明”);
$writer->endTag(“结果”);
}
$writer->endTag(“测试”);
$writer->characters(“\n”);
}
###****************************************************************
$writer->endTag(“测试”);
$writer->characters(“\n”);
$writer->endTag(“摘要”);
$writer->end();
$output->close();
}
###****************************************************************
&WriteXML($dir);
系统(“summary.xml”);
如前所述,读取XML文件的目录是另一个驱动器(此处D:\
)。如果我将所有XML文件放在Perl代码所在的同一文件夹中,那么效果很好,但在其他情况下,我会出现错误:
无法为文件“test.xml”创建文件解析器上下文:test.pl第46行的I/O控制操作不正确
请告诉我在哪里遗漏了导致错误的内容,好吗?更新
我道歉。我以为我已经回答了这个问题,因为你用完整的代码更新了它。我想我打错了,把它扔掉了
问题是这条线
my @testFile = <*.xml>
您还应在程序顶部使用autodie,它将自动捕获文件IO中的任何错误,如打开、关闭、chdir等,而无需编写代码或死亡$代码>在每一个上
原始响应 如果我把所有XML文件和perl代码放在同一个文件夹中,那么它就非常有用 出于许多原因,我认为您发布的代码根本无法工作,无论您将XML数据文件放在何处。以下是一些您需要解决的问题
- 您在任何地方都不会使用XML::Writer
- 您不需要调用
WriteXML
- 错误在程序的第144行,但代码只有30行
- 您打开
进行输出,但从未关闭它,因此在使用test.xml
xml::LibXML
- 您正在编写的数据只是
my ($dir) = @_; chdir $dir; my @testFile = <*.xml>;