我正在比较两个XML文件。如果发现其中一个文件中缺少一个节点,则要将其插入到另一个文件中。下面是我一直在尝试的方法:
my $out_file = 'fbCI_report.xml';
open my $fh_out, '>>', $out_file or die "Can't open $out_file for writing: $!";
my $currentReport = XML::Twig->new( pretty_print => 'indented' );
$currentReport->parsefile($path_to_currentReport);
print "Loaded current report.\n";
my $newReport = XML::Twig->new( pretty_print => 'indented' );
$newReport->parsefile($path_to_newReport);
print "Loaded new report.\n";
my $currentRoot = $currentReport->root; # get the root
my $currentBuilds = $currentRoot->first_child(); # get the builds node
my $currentXCR = $currentBuilds->first_child(); # get the xcr node
my $newRoot = $newReport->root; # get the root
my $newBuilds = $newRoot->first_child(); # get the builds node
my $newXCR = $newBuilds->first_child(); # get the xcr node
my @currentXCRarray = $currentBuilds->children('xcr');
my @newXCRarray = $newBuilds->children('xcr');
my $numberOfxcr = $newBuilds->children_count();
foreach my $currentXCRmod ( @currentXCRarray ) {
my $currentID = $currentXCRmod->att("id");
foreach my $newXCRmod (@newXCRarray) {
my $newID = $newXCRmod->att("id");
if ( $newID == $currentID ) {
last;
}
elsif ( $count == $numberOfxcr && $newID != $currentID ) {
my $insert = $currentBuilds->insert_new_elt($newXCRmod);
print "XCR does not exist in current report, adding it..\n";
}
$count++;
}
}
print $fh_out $currentReport->sprint();
close $fh_out;但是,这并不是插入带有相应子节点的节点,而是我猜是对节点的引用:<XML::Twig::Elt=HASH(0x326efe0)/>。是否有正确插入节点的方法?我还没有在CPAN网站上找到任何东西。
样本数据,current.xml:
<project>
<builds>
<xcr id="13367" buildable="false">
<artifact name="rb"/>
<artifact name="syca"/>
</xcr>
<xcr id="13826" buildable="false">
<artifact name="dcs"/>
</xcr>
<\builds>
<\project>new.xml:
<project>
<builds>
<xcr id="13367" buildable="false">
<artifact name="rb"/>
<artifact name="syca"/>
</xcr>
<xcr id="13826" buildable="false">
<artifact name="dcs"/>
</xcr>
<xcr id="10867" buildable="true">
<artifact name="smth"/>
<artifact name="top"/>
<artifact name="tree"/>
</xcr>
<\builds>
<\project>发布于 2016-07-21 12:20:33
你说得对--这是一个XML::Twig::Elt的紧张文本。
问题是- insert_new_elt创建了一个新元素。因此,您要做的是有效地“打印”元素id ( XML::Twig::Elt=HASH(0x326efe0))并创建一个名为该元素的新节点。
但你不想那样做-你想要复制一个现有的。
所以我建议你要做的是:
my $copied_elt = $currentXCRmod -> copy;
$copied_elt -> paste ( last_child => $currentBuilds );它将传递元素(进入“last_child”位置)。
尽管我建议您的循环可能也是您可以改进的东西--我建议您查看一个twig_handler,以检查解析文件中存在哪个ID:
my %seen_id;
sub collect_ids {
my ( $twig, $element ) = @_;
$seen_id { $element->att('id') } ++;
} 然后在解析时调用它:
my $currentReport = XML::Twig->new(twig_handlers => { 'xcr' => \&collect_ids},
pretty_print=>'indented');
$currentReport->parsefile($path_to_currentReport);这将使您可以轻松地比较/复制哪些存在或不存在。
或者(基于到目前为止的XML示例):
#!/usr/bin/env perl
use strict;
use warnings 'all';
use Data::Dumper;
use XML::Twig;
my $current = XML::Twig -> new ( ) -> parsefile ('test1.xml');
my $new = XML::Twig -> new ( ) -> parsefile ( 'test2.xml');
my $cur_builds = $current -> root -> get_xpath('./builds',0);
foreach my $xcr ( $new -> findnodes('//xcr') ) {
my $id = $xcr -> att('id');
if ( not $current -> findnodes("//xcr[\@id=\"$id\"]") ) {
print "$id not in current, copying\n";
my $copy = $xcr -> copy;
$copy -> paste ( last_child => $cur_builds );
}
}
$current -> set_pretty_print('indented_a');
$current -> print;发布于 2016-07-21 12:20:34
您可能应该移动节点(我不记得当您尝试插入一个已经是树的一部分的元素时会发生什么)。因此,编写$newXCRmo->move( first_child( $currentBuilds))并查看这是否改善了这种情况。
我没有太多时间查看您的代码,因此可能会有其他问题。
发布于 2016-07-21 13:18:11
你有你的比较循环“内翻”
此外,测试$count == $numberOfxcr永远不会成功,因为循环foreach my $newXCRmod (@newXCRarray)将在这之前终止。
下面是您的代码的一个改进版本,它使用XPath表达式和来自List::Util的any使循环更加简洁
use strict;
use warnings 'all';
use XML::Twig;
use List::Util 'any';
my ( $path_to_curr_report, $path_to_new_report ) = qw/ current.xml new.xml /;
my $out_file = 'fbCI_report.xml';
my $curr_report = XML::Twig->new->parsefile($path_to_curr_report);
my $new_report = XML::Twig->new->parsefile($path_to_new_report);
my ($curr_builds) = $curr_report->findnodes('/project/builds');
for my $new_xcr_mod ( $new_report->findnodes('/project/builds/xcr') ) {
my $new_id = $new_xcr_mod->att('id');
next if any { $new_id eq $_->att('id') } $curr_report->findnodes('/project/builds/xcr');
print qq{XCR with ID "$new_id" does not exist in current report. Adding it.\n};
$new_xcr_mod->copy->paste( last_child => $curr_builds );
}
{
$curr_report->set_pretty_print('indented');
open my $fh, '>', $out_file or die "Can't open $out_file for writing: $!";
$curr_report->print($fh);
close $fh;
}输出
XCR with ID "10867" does not exist in current report. Adding it.<project>
<builds>
<xcr buildable="false" id="13367">
<artifact name="rb"/>
<artifact name="syca"/>
</xcr>
<xcr buildable="false" id="13826">
<artifact name="dcs"/>
</xcr>
<xcr buildable="true" id="10867">
<artifact name="smth"/>
<artifact name="top"/>
<artifact name="tree"/>
</xcr>
</builds>
</project>https://stackoverflow.com/questions/38503943
复制相似问题