2

我不确定我的代码是否有问题,或者 XML::Twig 3.40 是否存在问题。目标是将method="MODIF" “更改或添加”到节点“attributez”的父节点

文件输入.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;

现在输出正常

于 2012-07-23T14:28:01.940 回答