There is a method in XML::Twig that does just what you want: erase.
Below is an example, with a couple of ways to solve the problem. I am sure there are others.
#!/usr/bin/perl
use strict;
use warnings;
use autodie qw(open);
use Test::More;
use XML::Twig;
my( $in, $expected)= do { local $/="\n\n"; <DATA>; };
is( remove_policy_rule( $in), $expected, 'simplest code');
is( with_flush( $in), $expected, 'with flush');
done_testing();
sub remove_policy_rule
{ my( $in)= @_;
my $t= XML::Twig->new( twig_handlers => { policyrules => sub { $_-
+>erase; } },
pretty_print => 'indented',
)
->parse( $in);
return $t->sprint;
}
sub with_flush
{ my( $in)= @_;
my $out;
open( my $fh, '>', \$out);
my $t= XML::Twig->new( twig_handlers => { policyrules => sub { $_-
+>erase;
$_[
+0]->flush( $fh);
}
},
pretty_print => 'indented',
)
->parse( $in);
return $out;
}
__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<policy name="Cur_policy" version="3.2.1">
<policyrules>
<Rule Action="allow" Enabled="true" RuleID="F68E32">
<source addr="10.4.5.3" srcport="any"/>
<dest addr="199.5.4.3" destport="80"/>
</Rule>
<Rule Action="allow" Enabled="true" RuleID="78E21D">
<source addr="10.4.0.0-10.4.255.255" srcport="any"/>
<dest addr="any" destport="80"/>
</Rule>
</policyrules>
</policy>
<?xml version="1.0" encoding="UTF-8"?>
<policy name="Cur_policy" version="3.2.1">
<Rule Action="allow" Enabled="true" RuleID="F68E32">
<source addr="10.4.5.3" srcport="any"/>
<dest addr="199.5.4.3" destport="80"/>
</Rule>
<Rule Action="allow" Enabled="true" RuleID="78E21D">
<source addr="10.4.0.0-10.4.255.255" srcport="any"/>
<dest addr="any" destport="80"/>
</Rule>
</policy>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|