使用XML建立图书信息管理表books.xml
tiger506@darkstar:~/Program/Perl$ cat ./books.xml
<bookstore>
<book>
<category>COOKING</category>
<lang>en</lang>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book>
<category>CHILDREN</category>
<lang>en</lang>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<category>WEB</category>
<lang>en</lang>
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
使用PERL语言编程,使图书信息格式化输出:
tiger506@darkstar:~/Program/Perl$ cat ./pbooks.pl
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xml_book="/home/tiger506/Program/Perl//books.xml";
my $book_info=XMLin($xml_book);
print Dumper($book_info); #查看翻译过来的图书数据结构
for($i=0;$i<3;$i++){
print "------------------------------------------------\n";
print "TITLE: ".$book_info->{book}->[$i]->{title}."\n";
print "LANG: ".$book_info->{book}->[$i]->{lang}."\n";
print "AUTHOR ".$book_info->{book}->[$i]->{author}."\n";
print "CATEGORY ".$book_info->{book}->[$i]->{category}."\n";
print "PRICE ".$book_info->{book}->[$i]->{price}."\n";
print "YEAR ".$book_info->{book}->[$i]->{year}."\n";
print "------------------------------------------------\n";
}
结果演示:
tiger506@darkstar:~/Program/Perl$ ./pbooks.pl
$VAR1 = {
'book' => [
{
'lang' => 'en',
'price' => '30.00',
'author' => 'Giada De Laurentiis',
'title' => 'Everyday Italian',
'category' => 'COOKING',
'year' => '2005'
},
{
'lang' => 'en',
'price' => '29.99',
'author' => 'J K. Rowling',
'title' => 'Harry Potter',
'category' => 'CHILDREN',
'year' => '2005'
},
{
'lang' => 'en',
'price' => '39.95',
'author' => 'Erik T. Ray',
'title' => 'Learning XML',
'category' => 'WEB',
'year' => '2003'
}
]
};
------------------------------------------------
TITLE: Everyday Italian
LANG: en
AUTHOR Giada De Laurentiis
CATEGORY COOKING
PRICE 30.00
YEAR 2005
------------------------------------------------
------------------------------------------------
TITLE: Harry Potter
LANG: en
AUTHOR J K. Rowling
CATEGORY CHILDREN
PRICE 29.99
YEAR 2005
------------------------------------------------
------------------------------------------------
TITLE: Learning XML
LANG: en
AUTHOR Erik T. Ray
CATEGORY WEB
PRICE 39.95
YEAR 2003
------------------------------------------------