| XML::Xml - XML Parser and Generator for Perl |
parse()read()elements()args()string()/print()
Xml - Perl extension for parsing and generating Xml documents. (nonvalidating)
use XML::Xml; use strict;
my $author = Xml->parse("
<author>
<fname>Seyhan</fname>
<lname>Ersoy</lname>
</author>
");
my $fname = $author->fname; my $lname = $author->lname;
print "$fname $lname \n";
Xml module can be used to parse an XML document or to create one.
use XML::Xml;
$xml_str = "
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
</books>
";
my $books = Xml->parse($xml_str);
my $book = $books->book;
my $title = $book->title; my $isbn = $book->isbn;
print "$title\n"; print "$isbn\n";
This will output:
Perl Cookbook 1-56592-243-3
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. Leaf object is transparent to the user, if the user follows certain conventions.
The following example shows how to handle multiple elements with the same name:
use XML::Xml;
$xml_str = "
<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>
";
my $books = Xml->parse($xml_str);
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";
This will output:
Perl Cookbook 1-56592-243-3 Advenced Perl Programming 1-56592-220-4
If there is one element you can still treat it as an array in your code:
$xml_str = "
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
</book>
";
my $books = Xml->parse($xml_str);
my @book = $books->book;
foreach $book ( @book ) {
print $book->title,"\n";
print $book->isbn,"\n";
}
This will output:
Perl Cookbook 1-56592-243-3
The example below demonstrates a simple xml document which contains some elements with attributes.Author element now has two attributes called age and height. Note that title and isbn elements do not have any attributes. If an element does not have any atribute, the method carresponding to element tag name returns a string which contains the value of the element. However, if an element has any attribute, than return value of same method is a Leaf object. The value method of this object returns the value of the element. The attribute values are returned by methods of Leaf object carresponding to attribute names. For example name attribute's value returned by name method. Similarly age attribute's value returned by age method. Following example demonstrates this:
use XML::Xml;
$xml_str = "
<books>
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
<author height = '175cm' age = '30'>Tom Cristiansen</author>
<author age = '31' height = '183cm'>Nathan Torkington</author>
</book>
<book>
<title>Advenced Perl Programming</title>
<isbn>1-56592-220-4</isbn>
<author age = '32' height = '170cm' >Sriram Srinivasan</author>
</book>
<book>
<title>Mastering Algorithms with Perl</title>
<isbn>1-56592-398-7</isbn>
<author height = 'unknown' age = '33'>Jon Orwant</author>
<author age = '34' height = '180cm'>Jarko Hietaniemi</author>
<author age = '35' height = 'unknown'>John Macdonald</author>
</book>
</books>
";
my $books = Xml->parse($xml_str);
my @book = $books->book;
foreach $bk ( @book ) {
print $bk->title,"\n";
print $bk->isbn,"\n";
my @author = $bk->author;
foreach $auth ( @author ) {
print "NAME = ",$auth->value,"\n";
print "AGE = ",$auth->age,"\n";
print "HEIGHT = ",$auth->height,"\n";
}
print "----------------------------\n";
}
This will output:
Perl Cookbook 1-56592-243-3 NAME = Tom Cristiansen AGE = 30 HEIGHT = 175cm NAME = Nathan Torkington AGE = 31 HEIGHT = 183cm ---------------------------- Advenced Perl Programming 1-56592-220-4 NAME = Sriram Srinivasan AGE = 32 HEIGHT = 170cm ---------------------------- Mastering Algorithms with Perl 1-56592-398-7 NAME = Jon Orwant AGE = 33 HEIGHT = unknown NAME = Jarko Hietaniemi AGE = 34 HEIGHT = 180cm NAME = John Macdonald AGE = 35 HEIGHT = unknown ----------------------------
The example below shows how to create a Xml structure which contains the ``books'' objects defined in the previous documentation. For this case a Xml object is created by using the new method of Xml.pm class. This method takes one argument that is the name of the fist element of the Xml structure we want to create. For example for books structure this will be ``books''. Once we created this object, we can call its methods to set the values of given elements and their attributes. The names of these methods are the name of the elements. These methods take one or two arguments. The first argument is the value of the element we want to set. The second argument is a hash reference and is used to set the values of attributes. The keys of this hash reference are the names of the attributes we want to set.
use XML::Xml;
my $books = Xml->new(books);
my $book1 = Xml->new(book);
$book1->title("Perl Cookbook");
$book1->isbn("1-56592-243-3");
$book1->author("Tom Cristiansen", { age => 30, height => "175cm" });
$book1->author("Nathan Torkington", { age => 31, height => "183cm" });
my $book2 = Xml->new(book);
$book2->title("Advenced Perl Programming");
$book2->isbn("1-56592-220-4");
$book2->author("Sriram Srinivasan", { age => 32, height => "170cm" });
my $book3 = Xml->new(book);
$book3->title("Mastering Algorithms with Perl");
$book3->isbn("1-56592-398-7");
$book3->author("Jon Orwant", { age => 33, height => "175cm" });
$book3->author("Jarko Hietaniemi", { height => "196cm" });
$book3->author("John Macdonald", { age => 35 });
$books->book($book1);
$books->book($book2);
$books->book($book3);
print $books->string, "\n";
This will output:
<books>
<book>
<title>
Perl Cookbook
</title>
<isbn>
1-56592-243-3
</isbn>
<author age = "30" height = "175cm">
Tom Cristiansen
</author>
<author age = "31" height = "183cm">
Nathan Torkington
</author>
</book>
<book>
<title>
Advenced Perl Programming
</title>
<isbn>
1-56592-220-4
</isbn>
<author age = "32" height = "170cm">
Sriram Srinivasan
</author>
</book>
<book>
<title>
Mastering Algorithms with Perl
</title>
<isbn>
1-56592-398-7
</isbn>
<author age = "33" height = "175cm">
Jon Orwant
</author>
<author height = "196cm">
Jarko Hietaniemi
</author>
<author age = "35">
John Macdonald
</author>
</book>
</books>
parse()Parses an XML string and returns an object.
my $books = Xml->parse($xml_str);
read()Opens and reads the file given in the argument and parses the XML contents in the file and returns an object.
my $books = Xml->parse($xml_file_name);
elements()Returns all the elements for the object as a hash reference.
args()Returns all the arguments for the object as a hash reference.
Example:
use XML::Xml;
$xml_str = "
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
<author height = '175cm' age = '30'>Tom Cristiansen</author>
<author age = '31' height = '183cm'>Nathan Torkington</author>
</book>
";
my $book = Xml->parse($xml_str);
$elements = $book->elements;
foreach $k (keys %{$elements}) {
foreach $e (@{$elements->{$k}}) {
print "$k = ",$e->value, "\n";
$args = $e->args;
foreach $a (keys %{$args}) {
print "$a = ", $args->{$a}, "\n";
}
print "---------------------------------\n";
}
}
This will output:
author = Tom Cristiansen height = 175cm age = 30 --------------------------------- author = Nathan Torkington age = 31 height = 183cm ------------------------------ --- isbn = 1-56592-243-3 ------------------------------ --- title = Perl Cookbook ------------------------------ ---
string()/print()Will output the XML object as a nicely formatted XML string.
Example:
use XML::Xml;
use Data::Dumper::Simple;
$xml_str = "
<book>
<title>Perl Cookbook</title>
<isbn>1-56592-243-3</isbn>
<author height = '175cm' age = '30'>Tom Cristiansen</author>
<author age = '31' height = '183cm'>Nathan Torkington</author>
</book>
";
my $book = Xml->parse($xml_str);
print $book->string; #or $book->print;
This will output:
<book>
<title>
Perl Cookbook
</title>
<isbn>
1-56592-243-3
</isbn>
<author height = "175cm" age = "30">
Tom Cristiansen
</author>
<author age = "31" height = "183cm">
Nathan Torkington
</author>
</book>
Seyhan Ersoy, seyhan@rcn.com Documentation of Xml is located at. http://users.rcn.com/seyhan
Stub documentation for Xml was created by h2xs. Xml has many futures to parse and create Xml documents. Since it has many futures, I wanted to show all the futures of this module by using simple programs. This programs also help me to find bugs. After a change in the module I run these programs for possible bugs. You can find these programs at
If you have any question, or you want to have simple program to show how the particular future of Xml is used, send me an e-mail at
You may use and distribute Xml module under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
I would like to acknowledge the valuable contributions of the many people I have worked with. I also thank my wife Zeynep Ersoy for her patience and support.
The Xml is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
perl(1).
| XML::Xml - XML Parser and Generator for Perl |