• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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

0
TonyJix
Asked:
TonyJix
  • 2
1 Solution
 
Adam314Commented:

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now