I’m trying to parse some XML in Perl using XML::Simple.
The XML follows a format of:
<result>
<doc>
<field name="title">Sample Title</field>
<field name="content">Content 1</field>
<field name="content">Content 2</field>
.
.
.
<field name="content">Content n</field>
</doc>
</result>
Using XML::Simple, I attempted to parse this and print the title and all content. The problem was that only the last content item was being printed. I decided to user Dumper, and this is what it returns:
$VAR1= {
'result'=> {
'doc' => [
{
'field' => {
'content' => {
'content' => 'Content n'
},
'title' => {
'content' => 'Sample Title'
}
}
}
Only the last content item is shown for each doc element. Is there any reason for this? What can I do to have it detect all of the content items?
Here’s the code:
my $url = "http://www.testurl.com/test.xml";
my $content = get $url;
die "Couldn't get XML" unless defined $content;
my $xml = new XML::Simple;
my $xmlData = $xml->XMLin($content);
print Dumper($xmlData);
Per the POD:
It’s taking “name” as an ID-type field and folding the elements together.