我的 XML 文件看起来像这样:
<doc>
<RU1>
<conf>
<prop name="a" val="http://a.org/a.html>
</conf>
</RU1>
<RAU1>
<conf>
<prop name="a" val="http://a.org/a.html>
</conf>
</RAU1>
<RU2>
<conf>
<prop name="a" val="http://a.org/a.html>
</conf>
</RU2>
</doc>
我想将 perl 中以 RU 开头的所有父标签下的 prop 字段值中的“a.org”替换为“b.com”。如何以 xml 文件形式获取更改?
假设您的 XML 格式良好(事实并非如此),您可以使用多个 CPAN 模块为了工作。大多数工作将涉及解析文档、使用 XPath 查询找到您的部分,然后再次打印文档。
这是 XML::Twig 的示例。我必须修复 XML 才能对其进行解析。
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new(
twig_handlers => {
'conf/prop' => sub { $_->{att}{val} =~ s/a.org/b.org/; }
},
pretty_print => "indented"
);
$twig->parse(join "", <DATA>);
$twig->print;
__END__
<foo>
<RU1>
<conf>
<prop name="a" val="http://a.org/a.html" />
</conf>
</RU1>
<RAU1>
<conf>
<prop name="a" val="http://a.org/a.html" />
</conf>
</RAU1>
<RU2>
<conf>
<prop name="a" val="http://a.org/a.html" />
</conf>
</RU2>
</foo>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)