1

$upd_dev_id が等しくない場合、mainea.xml に updev.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
  }
4

1 に答える 1

1

挿入は、あなたが思っていることをしません。要素を挿入しません。既存の要素の下にタグを挿入します。あなたが望むのはペーストです。

比較:

挿入 ($tag1, [$optional_atts1], $tag2, [$optional_atts2],...)

リスト内の各タグに対して、要素の唯一の子として要素 $tag を挿入します。この要素は、「$optional_atts」でオプションの属性を取得します。要素のすべての子は、新しい要素の子として設定されます。上位レベルの要素が返されます。

と:

貼り付け ($optional_position, $ref)

(以前に「カット」された、または新しく生成された) 要素を貼り付けます。要素がすでにツリーに属している場合は終了します。

新しいツリーに貼り付ける前に、おそらく要素を切り取るかコピーする必要があります。

于 2010-08-14T12:59:12.257 に答える