当id不相等时插入元素

发布于 2024-09-14 09:57:04 字数 1058 浏览 5 评论 0原文

如果 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-21 09:57:04

insert 不会做你认为它会做的事情,它不会插入元素,而是在现有元素下插入标签。你想要的是粘贴。

比较:

插入($tag1,[$Optional_atts1],$tag2,[$Optional_atts2],...)

对于列表中的每个标记,插入一个元素 $tag 作为该元素的唯一子元素。该元素在“$Optional_atts”中获取可选属性。该元素的所有子元素都被设置为新元素的子元素。返回上层元素。

和:

粘贴($可选_位置,$ref)

粘贴(之前“剪切”或新生成的)元素。如果元素已经属于树,则死亡。

在将元素粘贴到新树中之前,您可能必须剪切或复制该元素。

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:

insert ($tag1, [$optional_atts1], $tag2, [$optional_atts2],...)

For each tag in the list inserts an element $tag as the only child of the element. The element gets the optional attributes in"$optional_atts." All children of the element are set as children of the new element. The upper level element is returned.

with:

paste ($optional_position, $ref)

Paste a (previously "cut" or newly generated) element. Die if the element already belongs to a tree.

You will probably have to cut, or copy the element before pasting it in the new tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文