2005-12-11

快速开始Perl XML:接口篇

来源: 本站收集整理 作者:佚名 评论 0 条
 

Writing

use XML::Simple;



require "files/camelid_links.pl";

my 蕀elid_links = get_camelid_data();



my $xsimple = XML::Simple->new();



print $xsimple->XMLout(\蕀elid_links,

                       noattr => 1,

                       xmldecl => '

这数据到文档的任务的条件要求暴露了XML::Simple的一个弱点:它没有答应我们决定hash里的哪个key应该作为元素返回和哪个key该作为属性返回。上面例子的输出虽然接近我们的输出要求但还远远不够。对于那些更喜欢将XML文档内容直接作为Perl数据结构操作,而且需要在输出方面做更细微控制的案例,XML::Simple和XML::Writer配合得很好。

如下例子说明了如何使用XML::Write来符合我们的输出要求。

use XML::Writer;



require "files/camelid_links.pl";

my 蕀elid_links = get_camelid_data();



my $writer = XML::Writer->new();



$writer->xmlDecl();

$writer->startTag('html');

$writer->startTag('body');



foreach my $item ( keys (蕀elid_links) ) {

    $writer->startTag('a', 'href' => $camelid_links{$item}->{url});

    $writer->characters($camelid_links{$item}->{description});

    $writer->endTag('a');

}



$writer->endTag('body');

$writer->endTag('html');



$writer->end();

XML::SimpleObject

XML::SimpleObject 对XML数据使用回想文档对象模型(Document Object Model)的存取器/accessor来提供一个面对对象接口。

Reading

use XML::Parser;

use XML::SimpleObject;



my $file = 'files/camelids.xml';



my $parser = XML::Parser->new(ErrorContext => 2, Style => "Tree");

my $xso = XML::SimpleObject->new( $parser->parsefile($file) );



foreach my $species ($xso->child('camelids')->children('species')) {

    print $species->child('common-name')->{VALUE};

    print ' (' . $species->attribute('name') . ') ';

    print $species->child('conservation')->attribute('status');

    print "\n";

}

Writing

XML::SimpleObject 没有通过抓取来创建XML文档的功能。但是与上面的XML::Simple例子一样,可以通过与XML::Writer配合简单的完成任务。

XML::TreeBuilder

XML::TreeBuilder 包由两模块组成:XML::Element用来创建和获得XML元素点的内容和XML::TreeBuilder作为一个工厂包从已有XML文件中简化文档树的创建。对于那些已有值得尊敬的 HTML::Element 和 HTML::Tree 模块使用经验的人来说,使用 XML::TreeBuilder 是非常轻易的,因为除了XML特有的方法外其他都是一样的。

Reading

use XML::TreeBuilder;



my $file = 'files/camelids.xml';

my $tree = XML::TreeBuilder->new();



$tree->parse_file($file);



foreach my $species ($tree->find_by_tag_name('species')){

    print $species->find_by_tag_name('common-name')->as_text;

    print ' (' . $species->attr_get_i('name') . ') ';

    print $species->find_by_tag_name('conservation')->attr_get_i('status');

    print "\n";

}

Writing

use XML::Element;



require "files/camelid_links.pl";

my 蕀elid_links = get_camelid_data();





my $root = XML::Element->new('html');

my $body = XML::Element->new('body');

my $xml_pi = XML::Element->new('~pi', text => 'xml version="1.0"');

$root->push_content($body);



foreach my $item ( keys (蕀elid_links) ) {

    my $link = XML::Element->new('a', 'href' => $camelid_links{$item}->{url});

    $link->push_content($camelid_links{$item}->{description});

    $body->push_content($link);

}



print $xml_pi->as_XML;

print $root->as_XML();




共4页: 上一页 [1] [2] 3 [4] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:hao123站长李兴平的成功史  
下一篇:当客网:我们更注重用户体验
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号