I did a exercise like this, how do I calculate the # of XML elements collapsed into an array by XML::Simple so I don t have to hard-code the # of elements? I plan to use the code to parse a bigger xml file. I don t want to cout the elements by manual.
请允许我用一些数字取代神学院的号码,如<条码>人。 我知道,我可以方便地利用C#中的这种发言。
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
my $tree = XMLin( ./t1.xml );
print Dumper($tree);
print "
";
for (my $i = 0; $i < 2; $i++) # magic number 2
{
print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}
";
print "
";
for (my $j = 0; $j < 3; $j++) # magic number 3
{
print $tree->{person}->[$i]->{hobbie}->[$j], "
";
}
print "
";
}
概述:
could not find ParserDetails.ini in C:/Perl/site/lib/XML/SAX
$VAR1 = {
person => [
{
hobbie => [
bungy jumping ,
sky diving ,
knitting
],
last_name => Bloggs ,
first_name => Joe
},
{
hobbie => [
Swim ,
bike ,
run
],
last_name => LIU ,
first_name => Jack
}
]
};
Joe Bloggs
bungy jumping
sky diving
knitting
Jack LIU
Swim
bike
run
我的Xml来源档案如下:
<Document>
<person>
<first_name>Joe</first_name>
<last_name>Bloggs</last_name>
<hobbie>bungy jumping</hobbie>
<hobbie>sky diving</hobbie>
<hobbie>knitting</hobbie>
</person>
<person>
<first_name>Jack</first_name>
<last_name>LIU</last_name>
<hobbie>Swim</hobbie>
<hobbie>bike</hobbie>
<hobbie>run</hobbie>
</person>
</Document>