2012-11-01 20 views
0

我現在讀xml文件,我沒有特別的標記註釋,但它省略,然後插入評論...使用commet一些標籤在一個XML文件

XML:

<book> 
    <book-meta> 
    <book-id pub-id-type="doi">1545</book-id> 
</book-meta> 
</book> 

腳本:

use strict; 
use XML::Twig; 
open(my $out, '>', 'Output.xml') or die "can't Create stroy file $!\n"; 

my $story_file = XML::Twig->new(
           twig_handlers =>{ 
             'book-id' => sub {$_->set_comment('drop')}, 
             keep_atts_order => 1, 
             }, 
             pretty_print => 'indented', 
          ); 

$story_file->parsefile('sample.xml'); 
$story_file->print($out); 

我的輸出是:

<book> 
    <book-meta><!--drop--> 
    </book-meta> 
</book> 

我需要出去爲:

<book> 
    <book-meta> 
    <!--<book-id pub-id-type="doi">1545</book-id>--> 
</book-meta> 
</book> 

我錯在這個過程中....

回答

1

只需設置註釋文本到樹枝的XML:

'book-id' => sub { $_->set_comment($_->sprint) }, 
+0

感謝ü它工作得很好 – user1787633

+0

@ user1787633:「謝謝」通過接受和/或加強答案來表達。 – choroba

相關問題