XML的相关部分看起来像
<?xml version="1.0" encoding="UTF-8"?>
<key name="home">
<entry name="default">zzzzz</entry>
<key name="ffffff"></key>
<key name="plugin">
<key name="pst">
<entry name="enable">true</entry>
</key>
</key>
<key name="hover">
<key name="doze">
<key name="eventmanager">
<key name="types"></key>
</key>
<key name="ips">
<key name="proto">
<key name="dos">
<key name="dop"></key>
</key>
</key>
</key>
</key>
</key>
.
.
.
我的代码:
#!/root/bin/perl
use strict;
use warnings;
use XML::Twig;
my $tree = XML::Twig->new(pretty_print => 'indented', keep_spaces => 1);
$tree->parsefile(shift);
my $msg =<<'MSG';
<key name="absy">
<entry name="msg"></entry>
<entry name="title"></entry>
</key>
MSG
my $client_plugin = XML::Twig::Elt->new( key => { name => 'client' } )->set_inner_xml($msg);
$client_plugin->paste(last_child => $tree->first_elt(sub { $_[0]->tag =~ m/key/ and $_[0]->att('name') =~ m/plugin/i }));
$tree->print;
问题是添加的元素出现在一行中,并且不像其他元素那样真正缩进。我也试过$tree->print(pretty_print => 'indented')
没有运气。