当id不相等时插入元素
如果 $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
}
I would like to insert updev.xml to the mainea.xml if $upd_dev_id is not equal. Tried this below but it won't insert. Replace works.
#!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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
insert 不会做你认为它会做的事情,它不会插入元素,而是在现有元素下插入标签。你想要的是粘贴。
比较:
和:
在将元素粘贴到新树中之前,您可能必须剪切或复制该元素。
insert doesn't do what you think it does, it doesn't insert an element, it inserts a tag under an existing element. What you want is paste.
compare:
with:
You will probably have to cut, or copy the element before pasting it in the new tree.