【发布时间】:2014-10-11 01:23:10
【问题描述】:
我在解析 XML 时遇到了“不一致”(对我而言):
use 5.14.2;
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml;
{local $/;
$xml = <DATA>;}
my $xmlParsed = XMLin($xml,
KeyAttr => {phone => 'type', tankstelle => 'id'},
ForceArray => [ 'phone' ],
ContentKey => '-content',
);
say Dumper($$xmlParsed{'tankstelle'});
__DATA__
<?xml version="1.0"?>
<tankstellen>
<tankstelle>
<id>63</id>
<phone type="main">0911 731586</phone>
<phone type="fax">0911 7592228</phone>
<number/>
</tankstelle>
<tankstelle>
<id>64</id>
<phone type="main">0911 732011</phone>
<phone type="fax"></phone>
<number>64</number>
</tankstelle>
<tankstelle>
<id>91</id>
<phone type="main">0911 732926</phone>
<phone type="fax">0911 732917</phone>
<number/>
</tankstelle>
<tankstelle>
<id>92</id>
<phone type="main">0911 737577</phone>
<phone type="fax"></phone>
<number/>
</tankstelle>
</tankstellen>
有时数字是散列,有时是字符串。如果 type="fax" 为空,则它主要包含内容。
我为解析器尝试了不同的选项来摆脱 main 和 number 中的哈希,但没有运气。
'64' => {
'number' => '64',
'phone' => {
'main' => {
'content' => '0911 732011'
},
'fax' => {}
}
},
'91' => {
'phone' => {
'fax' => '0911 732917',
'main' => '0911 732926'
},
'number' => {}
}
【问题讨论】:
标签: perl xml-parsing