In this document we will explain how to use Xml.pm module. We are
not going to explain what is the XML, because you can find much documentation
about this subject in the net.
The module described here can be used to parse a xml document or generate
one. First we will explain how to parse an XML document, starting with
a simple case and gradually advancing to more complicated cases. To illustrate
how parsing works, we use a table for each case explained. This table contains
three rows. In the first row we display XML document we are going to parse.
This row of the table has lightgreen background. In the second row of the
Table we show the program which we are using for parsing. The background of
this cell is black. Finally in the third column we display the result of
the program which is given with lightyellow background.
Note that the content of the column one will be denoted as $xml in the
program given in column two.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
</books> |
use Xml; my $books = Xml->parse($xml); my $title = $books->book->title; my $isbn = $books->book->isbn; print "$title\n"; print "$isbn\n"; |
| Perl Cookbook 1-56592-243-3 |
In the program given above $books is a DOM (Document Object Model)
object. In the method chain
$books->book->title
except the last method, the methods return a DOM object or array of DOM
objects. The last method returns either a string containing the value of
corresponding element or a Leaf object. The Leaf object is used when values
contains attributes. This Leaf object usually will be transparent to the
user, if
the user follows certain conventions. More about Leaf objects in the next
documentation.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
</books> |
use Xml; my $books = Xml->parse($xml); my $book = $books->book; my $title = $book->title; my $isbn = $book->isbn; print "$title\n"; print "$isbn\n"; |
| Perl Cookbook 1-56592-243-3 |
In the program we used following string,
$books->book;
to get a single DOM object and we called it as $book. By using methods
of the $book object we can get title, isbn or any member that book object
contains. The previous program contains only one book object. However
in real application our Xml document contains more than one book object.
How are we going to parse a Xml document that contains multiple
occurrances of a given object?
The answer to this question is that, the book method in following string
$books->book
returns an array of book objects as stated before.
Program given below shows how we could retrieve information about
two-book objects.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
<book>
<title>Advenced Perl Programming</title>
<isbn>1-56592-220-4</isbn>
</book>
</books> |
use Xml; my $books = Xml->parse($xml); my @book = $books->book; my $title0= $book[0]->title; my $isbn0 = $book[0]->isbn; my $title1= $book[1]->title; my $isbn1 = $book[1]->isbn; print "$title0\n"; print "$isbn0\n"; print "$title1\n"; print "$isbn1\n"; |
| Perl Cookbook 1-56592-243-3 Advenced Perl Programming 1-56592-220-4 |
In the previous program we know in advance how many book objects our Xml document contains, and we wrote our program accordingly. However in many instances this value may not be known. In this case we can use a for loop as specified below.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
<book>
<title>Advenced Perl Programming</title>
<isbn>1-56592-220-4</isbn>
</book>
<book>
<title>Mastering Algorithms with Perl</title>
<isbn>1-56592-398-7</isbn>
</book>
</books> |
use Xml;
my $books = Xml->parse($xml);
my @book = $books->book;
for(my $i = 0; $i <= $#book; $i++ ) {
print $book[$i]->title,"\n";
print $book[$i]->isbn,"\n";
}
|
| Perl Cookbook 1-56592-243-3 Advenced Perl Programming 1-56592-220-4 Mastering Algorithms with Perl 1-56592-398-7 |
or we can use a foreach loop as specified below.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
<book>
<title>Advenced Perl Programming</title>
<isbn>1-56592-220-4</isbn>
</book>
<book>
<title>Mastering Algorithms with Perl</title>
<isbn>1-56592-398-7</isbn>
</book>
</books> |
use Xml;
my $books = Xml->parse($xml);
my @book = $books->book;
foreach $book ( @book ) {
print $book->title,"\n";
print $book->isbn,"\n";
}
|
| Perl Cookbook 1-56592-243-3 Advenced Perl Programming 1-56592-220-4 Mastering Algorithms with Perl 1-56592-398-7 |
How can we parse a Xml document if number of books are 0,1 or more?
As we seen before
$book->book;
returns an object if there is only one book object in the Xml document, or
an array of book objects if document contains more than one.
The answer to this question is to use foreach loop as shown below.
First program handles multiple book objects.
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
<book>
<title>Advenced Perl Programming</title>
<isbn>1-56592-220-4</isbn>
</book>
<book>
<title>Mastering Algorithms with Perl</title>
<isbn>1-56592-398-7</isbn>
</book>
</books> |
use Xml;
my $books = Xml->parse($xml);
my @book = $books->book;
foreach $book ( @book ) {
print $book->title,"\n";
print $book->isbn,"\n";
}
|
| Perl Cookbook 1-56592-243-3 Advenced Perl Programming 1-56592-220-4 Mastering Algorithms with Perl 1-56592-398-7 |
Same program with one object
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
</books> |
use Xml;
my $books = Xml->parse($xml);
my @book = $books->book;
foreach $book ( @book ) {
print $book->title,"\n";
print $book->isbn,"\n";
}
|
| Perl Cookbook 1-56592-243-3 |
Same program with no object
<books> </books> |
use Xml;
my $books = Xml->parse($xml);
my @book = $books->book;
foreach $book ( @book ) {
print $book->title,"\n";
print $book->isbn,"\n";
}
|