擅长:python、mysql、java
<p>更合适的方法是使用XML解析库,<a href="https://metacpan.org/pod/XML::LibXML" rel="nofollow">^{<cd1>}</a>是一个很好的方法。注意,您当前的示例是<em>不是</em>有效的XML:</p>
<pre><code>use strict;
use warnings;
use XML::LibXML;
my $xml_filename = $ARGV[0];
die "Missing name of xml file to parse"
unless $xml_filename;
open(my $xml_file, '<', $xml_filename)
or die "Error opening XML file: $!";
my $dom = XML::LibXML->load_xml(IO => $xml_file);
foreach my $node ( $dom->findnodes('/root/AVP') ) {
$node->unbindNode()
if $node->getAttribute('code') == 123;
}
open(my $out_fh, '>', "${xml_filename}.out")
or die "Unable to open outfile: $!";
binmode $out_fh;
# write XML tree to file
$dom->toFH($out_fh);
close($out_fh);
</code></pre>