XML Parser

Hello,

Could someone provide a code to print out the fields of every 'Stats'-entry from the following XML file. Thanks.
<MasterStats>
 
<Stats>
<field name="AdId">
	<value>1</value>
</field>
<field name="AdName">
	<value>A</value>
</field>
</Stats>
 
<Stats>
<field name="AdId">
	<value>2</value>
</field>
<field name="AdName">
	<value>B</value>
</field>
</Stats>
 
</MasterStats>

Open in new window

TonyJixAsked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:

use XML::Simple;
 
my $xml = XMLin("YourFileNameHere.xml");
print Dumper($xml);
 
foreach my $stat (@{$xml->{Stats}}) {
	foreach my $fieldname (keys %{$stat->{field}}) {
		print "Field '$fieldname' = '$stat->{field}->{$fieldname}->{value}'\n";
	}
}

Open in new window

0
 
Adam314Commented:
You can remove line 4:
    print Dumper($xml);
It was in for debugging.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.