Reputation: 8895
relevant part of the XML looks like
<?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>
.
.
.
my code:
#!/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;
the issue is that the added element appears in one single line, and not really indented like the rest of the elements.
I also tried $tree->print(pretty_print => 'indented')
with no luck.
Upvotes: 2
Views: 269
Reputation: 126762
The XML::Twig::Elt
module is discarding insignificant whitespace from your inner_html
call.
Meanwhile, you have specified keep_spaces => 1
, for the XML::Twig
module so it is retaining the (lack of) spaces as they are.
Remove keep_spaces => 1
and it should work for you.
Update
You may want to look at this code that does the same thing as your own program but uses an XPath step as a condition instead of the less-readable anonymous subroutine.
use strict;
use warnings;
use XML::Twig;
my $tree = XML::Twig->new(pretty_print => 'indented');
$tree->parsefile(shift);
my $msg = <<'MSG';
<key name="absy">
<entry name="msg"/>
<entry name="title"/>
</key>
MSG
my $client_plugin = XML::Twig::Elt->new( key => { name => 'client' } );
$client_plugin->set_inner_xml($msg);
my $plugin = $tree->first_elt('key[@name="plugin"]');
$client_plugin->paste(last_child => $plugin);
$tree->print;
output
<?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 name="client">
<key name="absy">
<entry name="msg"/>
<entry name="title"/>
</key>
</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>
</key>
Upvotes: 4