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>
which produces
--- 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.