2

コードに欠陥があるのか​​、XML ::Twig3.40に問題があるのか​​わかりません。目標は、method="MODIF"をノード"attributez"の親に「変更または追加」することです。

ファイルinput.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <world id="0" method="create">
     <continent id="0">
        <country id="18" method="create">
           <attributez>
              <info1>blabla</info1>
              <info2>blibli</info2>
           </attributez>
        </country>
     </continent>
   <attributez>
        <number_people>5billions</number_people>
        <number_continent>5</number_continent>
   </attributez>

     <oceans id="atlantic" method="create">
        <attributez>
           <name>ATLANTIC</name>
        </attributez>
     </oceans>

  </world>

私のコード(http://xmltwig.org/xmltwig/tutorial/yapc_twig_s4.html、例4から少し変更):

#!/bin/perl -w
use strict;
use XML::Twig;

my $twig= new XML::Twig( 
            twig_handlers =>                  # player will be called
              { attributez => \&player }         
                   );                        

$twig->parsefile( "input.xml");                     # build the twig
$twig->flush;                                     # flush the end of the twig  

sub player
{ my( $twig, $valeur)= @_;                      # handlers params are always
                                              # the twig and the element

($valeur-> parent)   ->set_att("method" => "MODIF");

$twig->flush;                                 # flush the twig so far   
}

ルートを次のように更新する必要があります。

 <world id="0" method="MODIF">

代わりに、次の出力を取得します(ルート属性はまったく更新されません)。

<?xml version="1.0" encoding="UTF-8"?>
<world id="0" method="create">
 <continent id="0">
    <country id="18" method="MODIF">
        <attributez>
            <info1>blabla</info1>
            <info2>blibli</info2>
        </attributez>
    </country>
</continent>

<attributez>
    <number_people>5billions</number_people>
    <number_continent>5</number_continent>
</attributez>

  <oceans id="atlantic" method="MODIF">
    <attributez>
        <name>ATLANTIC</name>
    </attributez>
  </oceans>
</world>
4

1 に答える 1

0

最後のもの(サブルーチンプレーヤー内のもの)を削除する必要がありました

 $twig->flush;

そして、出力はこれでOKです

于 2012-07-23T14:28:01.940 に答える