[英]perl script to print xml file data
How to print multiple number of trouble code and descripions using perl script.I have the xml file like this.. 如何使用perl脚本打印多个故障代码和说明。我有这样的xml文件。
<data>........
.......
</data>
lik this i have more number of trouble codes.still now i am printing only trouble code,how should i print descrption below the trouble code. 像这样我有更多的故障代码。仍然现在我只打印故障代码,我应该如何在故障代码下面打印说明。
Try this: 尝试这个:
#!/C:/Languages/Perl64/bin/perl.exe
use warnings;
use strict;
use XML::LibXML::Reader;
my $file;
open( $file, 'test.xml' );
my $reader = XML::LibXML::Reader->new( IO => $file ) or die( "unable to open file" );
while ( $reader->nextElement( 'DTC' ) ) {
my $description = $reader->readOuterXml();
$reader->nextElement( 'TroubleCode' );
my $troubleCode = $reader->readInnerXml();
print( "trouble code: $troubleCode\n" );
print( " description: $description\n" );
}
close( $file );
Okay, corrected and tested. 好,更正并测试。 This works per your question.
这适用于您的问题。
I answered the same question over on perlmonks so hopefully he finds an answer he likes. 我回答了关于perlmonks的相同问题,因此希望他能找到喜欢的答案。
use XML::Simple;
my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml");
my %by_code;
foreach my $dtc ( @{ $data->{DTC} } ) {
push @{ $by_code{ $dtc->{TroubleCode} } }, $dtc;
}
foreach my $code ( sort { $a <=> $b } keys %by_code ) {
print "trouble code: $code\n";
print "description:\n";
print map { $xml->XMLout( $_, RootName => 'DTC', NoAttr => 1, ) }
@{ $by_code{$code} };
}
Would you try this: 您可以尝试以下方法:
print " DTCnumber: \n" . join("\n", @dtcnumber);
and: 和:
print " DTCdes: \n" . join("\n", @dtcdes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.