There are lots of modules on CPAN that parse XML. You really don't want to do it yourself except as as a learning exercise. And then you will use a module from CPAN;)
When dealing with data structures it is very helpful to print the data you are working with. I've use YAML others use Data::Dumper, either is fine.
Update: I've changed the code below in light of ikegami's comments.
This is one approach using XML::Simple
#!/net/perl/5.10.0/bin/perl use strict; use warnings; use YAML; use XML::Simple; my $teams = XMLin( do { local $/; <DATA> }, ForceArray => [qw( football_team Player )], ); print Dump $teams; foreach my $team ( @{ $teams->{football_team} } ) { print "$team->{Name}\n"; foreach my $player ( @{ $team->{Players}{Player} } ) { print " $player->{ForeName} $player->{LastName}\n"; } } __DATA__
<football_teams> <football_team> <Name>ONE</Name> <DateCreated> <Year>1990</Year> <Month>12</Month> <Day>04</Day> </DateCreated> <Players> <Player> <LastName>Wonguso</LastName> <ForeName>Jimm</ForeName> <Initials>JW</Initials> </Player> <Player> <LastName>Fearless</LastName> <ForeName>A</ForeName> <Initials>AF</Initials> </Player> </Players> <Hometown>New York</Hometown> </football_team> <football_team> <Name>TWO</Name> <DateCreated> <Year>1978</Year> <Month>9</Month> <Day>23</Day> </DateCreated> <Players> <Player> <LastName>Pedro</LastName> <ForeName>Therry</ForeName> <Initials>TP</Initials> </Player> <Player> <LastName>Haywardis</LastName> <ForeName>Richie</ForeName> <Initials>RH</Initials> </Player> <Player> <LastName>Eswa</LastName> <ForeName>Jeyan</ForeName> <Initials>JE</Initials> </Player> <Player> <LastName>Leongus</LastName> <ForeName>John</ForeName> <Initials>JL</Initials> </Player> <Player> <LastName>Bentson</LastName> <ForeName>Billie</ForeName> <Initials>BB</Initials> </Player> </Players> <Hometown>California</Hometown> </football_team> </football_teams>
--- football_team: - DateCreated: Day: 04 Month: 12 Year: 1990 Hometown: New York Name: ONE Players: Player: - ForeName: Jimm Initials: JW LastName: Wonguso - ForeName: A Initials: AF LastName: Fearless - DateCreated: Day: 23 Month: 9 Year: 1978 Hometown: California Name: TWO Players: Player: - ForeName: Therry Initials: TP LastName: Pedro - ForeName: Richie Initials: RH LastName: Haywardis - ForeName: Jeyan Initials: JE LastName: Eswa - ForeName: John Initials: JL LastName: Leongus - ForeName: Billie Initials: BB LastName: Bentson ONE Jimm Wonguso A Fearless TWO Therry Pedro Richie Haywardis Jeyan Eswa John Leongus Billie Bentson
In reply to Re: problem in XML parsing
by hipowls
in thread problem in XML parsing
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |