输入 XML
<?xml version="1.0" encoding="utf-8"?>
<!--00/00/0000 12:35:25 AM-->
<Physical xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<Pro managementID="22000020">
<Identification Type="XXXXX" >
<Address>
<Data>test</Data>
</Address>
<Phone>
<Number>0000</Number>
</Phone>
<Email>test@com</Email>
</Identification>
</Pro>
</Physical>
脚本:
我正在尝试更改标签的值并将其余部分打印到新的输出 xml 文件中
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml = XML::Simple->new(ForceContent => 1,);
my $xmlData = $xml->XMLin('input.xml') or die$!;
print Dumper (\$xmlData);
foreach my $xmlKey ( keys %{$xmlData} ){
if ( $xmlKey =~ m/Pro/){
print ${$xmlData}{$xmlKey}{Identification}{Address}{Data}="hello";
}
}
XMLout(
$xmlData,
KeepRoot => 1,
NoAttr => 0,
OutputFile => $xml_out,
);
输出 XML:
<opt xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Pro managementID="22000020">
<Identification Type="XXXXX">
<Address Data="hello" />
<Email>test@com</Email>
<Phone name="Number">0000</Phone>
</Identification>
</Pro>
</opt>
我可以更改值,但是当我尝试将数据写入输出时,格式已更改。任何人都可以指导我获取具有更改值的相同输入数据作为输出。