Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

XML and Perl Chapter 2

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

2891 ch02  09.17.02  5:23 PM  Page 22
22
Chapter 2
Now Let’s Start Digging
Parsing an XML Document and
Extracting Statistics
Now that you’ve got a good understanding of what it means to parse an XML
document, let’s take a look at a short example that uses one of the tools that
were just discussed. For this example, we’ll use the XML::Parser Perl module
to parse a small XML document. Listing 2.1 shows a small XML document
that contains statistics from two great baseball players. As you can see, the
XML document is very simple. It has two
elements, and each
<player>
element has four child elements (
,
,
, and
<player>
<name>
<team>
<home_runs>
). Granted, this is a small and simple XML document, but it
<batting_average>
is perfect for illustrating simple XML parsing. Our goal for this example is to
parse this document and extract the statistics for each player.
Listing 2.1
Career statistics for two Hall of Fame baseball players stored in an
XML document. (Filename: ch2_baseball_stats.xml)
<?xml version=”1.0”?>
<career_statistics>
<player>
<name>Mickey Mantle</name>
<team>NY Yankees</team>
<home_runs>536</home_runs>
<batting_average>.298</batting_average>
</player>
<player>
<name>Joe DiMaggio</name>
<team>NY Yankees</team>
<home_runs>361</home_runs>
<batting_average>.325</batting_average>
</player>
</career_statistics>
We wrote a small application to parse this XML document. Don’t worry about
the source code yet, we’ll start off slow and start showing Perl applications a
little later in this chapter. For now, just focus on how an XML parser works,
and don’t be too concerned with all of the small details yet.
We used the XML::Parser Perl module to build an application to parse the
XML document. The XML::Parser Perl module is an event based parser that
will be discussed in Chapter 3, ŇEvent-Driven Parser Modules.” The output
generated by the Perl XML parsing application is shown in the following:
"/>
2891 ch02  09.17.02  5:23 PM  Page 22
22
Chapter 2
Now Let’s Start Digging
Parsing an XML Document and
Extracting Statistics
Now that you’ve got a good understanding of what it means to parse an XML
document, let’s take a look at a short example that uses one of the tools that
were just discussed. For this example, we’ll use the XML::Parser Perl module
to parse a small XML document. Listing 2.1 shows a small XML document
that contains statistics from two great baseball players. As you can see, the
XML document is very simple. It has two
elements, and each
<player>
element has four child elements (
,
,
, and
<player>
<name>
<team>
<home_runs>
). Granted, this is a small and simple XML document, but it
<batting_average>
is perfect for illustrating simple XML parsing. Our goal for this example is to
parse this document and extract the statistics for each player.
Listing 2.1
Career statistics for two Hall of Fame baseball players stored in an
XML document. (Filename: ch2_baseball_stats.xml)
<?xml version=”1.0”?>
<career_statistics>
<player>
<name>Mickey Mantle</name>
<team>NY Yankees</team>
<home_runs>536</home_runs>
<batting_average>.298</batting_average>
</player>
<player>
<name>Joe DiMaggio</name>
<team>NY Yankees</team>
<home_runs>361</home_runs>
<batting_average>.325</batting_average>
</player>
</career_statistics>
We wrote a small application to parse this XML document. Don’t worry about
the source code yet, we’ll start off slow and start showing Perl applications a
little later in this chapter. For now, just focus on how an XML parser works,
and don’t be too concerned with all of the small details yet.
We used the XML::Parser Perl module to build an application to parse the
XML document. The XML::Parser Perl module is an event based parser that
will be discussed in Chapter 3, ŇEvent-Driven Parser Modules.” The output
generated by the Perl XML parsing application is shown in the following:

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
electronic water conditioner
answering service
Gatlinburg Cabins
Pet Supplies
Wedding Favors
email security
online business degree
invoice factoring 
Accredited Degrees
Theme Parties
web design london
laser hair removal
Merchant Account
Home elevators
Color Laser Printer
Asset Protection
Offshore Banking
Offshore Merchant Account

Interested in Text ads?
1,733 Total Members
63 members(last 30 days)
11 members(last 7 days)
2 members(today)

1,436 Total Discussions
27 Posts(last 30 days)
5 Posts(last 7 days)
0 Posts(today)

5,400 Total Blog Posts
1,198 Blogs(last 30 days)
212 Blogs(last 7 days)
0 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,711 Total Resources
9 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139