如果$ upd_dev_id不相等,我想将updev.xml插入到mainea.xml中。试过这个,但不会插入。替换作品。
#!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
}
答案 0 :(得分:1)
insert不会按照您的想法执行,它不会插入元素,它会在现有元素下插入标记。你想要的是粘贴。
比较
insert($ tag1,[$ optional_atts1],$ tag2,[$ optional_atts2],...)
对于列表中的每个标记,插入元素$ tag作为元素的唯一子元素。该元素获取“$ optional_atts”中的可选属性。元素的所有子元素都设置为新元素的子元素。返回上层元素。
使用:
粘贴($ optional_position,$ ref)
粘贴(先前“剪切”或新生成的)元素。如果元素已经属于树,则死亡。
在将元素粘贴到新树中之前,您可能需要剪切或复制该元素。