1
如果$ upd_dev_id不相等,我想将updev.xml插入到mainea.xml中。尝试下面,但它不会插入。替换作品。当id不相等时插入元素
#!c:\perl\bin\perl.exe
use strict;
use XML::Twig;
my $upd_file = "updev.xml" ;
my $main_file = "mainea.xml" ;
# get the info we need by loading the update file
my $t_upd= new XML::Twig();
$t_upd->parsefile($upd_file);
my $upd_dev_id = $t_upd->root->next_elt('DEVNUM')->text;
my $upd_dev = $t_upd->root->next_elt('DEVS');
# now process the main file
my $t= new XML::Twig(TwigHandlers => { DEVS => \&DEVS, },
PrettyPrint => 'indented',
);
$t->parsefile($main_file);
$t->flush; # don't forget or the last closing tags won't be printed
sub DEVS
{ my($t, $DEVS)= @_;
# just replace devs if the previous dev_id is the right one
if($DEVS->prev_elt('DEVNUM')->text eq $upd_dev_id) {
$upd_dev->replace($DEVS);
}
else
{
$upd_dev->insert($DEVS);
}
$t->flush; # print and flush memory so only one job is in there at once
}
谢谢米罗德工作。必须先复制它。 – Cobra 2010-08-20 16:46:38