2012-07-23 40 views
2

我不确定我的代码是否有问题,或者是否存在XML :: Twig 3.40的问题。 目标是“改变或添加”方法= “MODIF”节点的父 “attributez”与我的代码,Perl :: Twig缺少一个父节点?

文件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> 

回答

0

,我不得不删除最后一个(的子程序内线球员之一)

$twig->flush; 

现在输出正常

+0

现在用更大的XML输入文件进行测试 – laurentngu 2012-07-23 14:28:42

+0

没关系。不需要进一步的答案 – laurentngu 2012-07-23 14:50:04