Hash 如下:
my 蕀elid_links = (
one => { url => '
http://www.online.discovery.com/news/picture/may99/photo20.html',
description => 'Bactrian Camel in front of Great ' .
'Pyramids in Giza, Egypt.'},
two => { url => 'http://www.fotos-online.de/english/m/09/9532.htm',
description => 'Dromedary Camel illustrates the ' .
'importance of accessorizing.'},
three => { url => 'http://www.eskimo.com/~wallama/funny.htm',
description => 'Charlie - biography of a narcissistic llama.'},
four => { url => 'http://arrow.colorado.edu/travels/other/turkey.html',
description => 'A visual metaphor for the perl5-porters ' .
'list?'},
five => { url => 'http://www.galaonline.org/pics.htm',
description => 'Many cool alpacas.'},
six => { url => 'http://www.thpf.de/suedamerikareise/galerie/vicunas.htm',
description => 'Wild Vicunas in a scenic landscape.'}
);
而我们所期望从hash中创建的文档例子为:
<?xml version="1.0">
<html>
<body>
<a href="http://www.eskimo.com/~wallama/funny.htm">Charlie -
biography of a narcissistic llama.</a>
<a href="http://www.online.discovery.com/news/picture/may99/photo20.html">Bactrian
Camel in front of Great Pyramids in Giza, Egypt.</a>
<a href="http://www.fotos-online.de/english/m/09/9532.htm">Dromedary
Camel illustrates the importance of accessorizing.</a>
<a href="http://www.galaonline.org/pics.htm">Many cool alpacas.</a>
<a href="http://arrow.colorado.edu/travels/other/turkey.html">A visual
metaphor for the perl5-porters list?</a>
<a href="http://www.thpf.de/suedamerikareise/galerie/vicunas.htm">Wild
Vicunas in a scenic landscape.</a>
</body>
</html>
良好缩进的XML结果文件(如上面所显示的)对于阅读很重要,但这种良好的空格处理不是我们案例所要求的。我们所关心的是结果文档是结构良好的/well-formed和它正确地表现了hash里的数据。 任务定义完毕,接下来该是代码例子的时候了。
use XML::Simple;
my $file = 'files/camelids.xml';
my $xs1 = XML::Simple->new();
my $doc = $xs1->XMLin($file);
foreach my $key (keys (%{$doc->{species}})){
print $doc->{species}->{$key}->{'common-name'} . ' (' . $key . ') ';
print $doc->{species}->{$key}->{conservation}->final . "\n";
}
评论加载中…
![]() |