2005-12-11

如何创建 Atom

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

简介

Atom 的作用请自行搜索。

描述

这里我使用 XML::Atom::SimpleFeed 来创建 Atom

XML::Atom::SimpleFeed 有些许瑕疵,如不能设置编码和多了个默认的 xml:lang="en"

我略微修改了下使之符合我的要求。

  1. 删除两处 xml:lang="en"
  2. 改 encoding="iso-8859-1" 为 encoding="UTF-8"

代码

如下代码来自我基于 Catalyst 的 Eplanet



use lib "E:/t/Eplanet/hackedlib"; # add the hack file's address

use XML::Atom::SimpleFeed;



my $Atom_file = $c->config->{build_root} . "/atom.xml";

my $title = 'Fayland\'s';

my $link = 'http://www.fayland.org/';

my $description = 'What Fayland says';

	

my $atom = XML::Atom::SimpleFeed->new(

	title    => $title,

	link     => $link,

	tagline  => $description,

	author => { name => 'Fayland Lam' },

)

or die;



my @cats = Eplanet::M::CDBI::Cms->retrieve_from_sql(qq{

	1=1 ORDER BY cms_id DESC LIMIT 0, 20

});



foreach my $cat (@cats) {

	my $_title = $cat->get('cms_title');

	my $_link = "http://www.fayland.org/journal/$cat->{'cms_file'}.html";

	my $_description = $cat->{'cms_describe'};

	my $_create_data = $cat->{'cms_cre_data'};

	# convert to the standard w3cdtf

	$_create_data = date2w3cdtf($_create_data);

	# got the modified data

	my $_modified_data = $cat->{'cms_mod_data'};

	# if not exists, use create data instead

	if ($_modified_data) {

		$_modified_data = date2w3cdtf($_modified_data);

	} else {

		$_modified_data = $_create_data;

	}

		

	$atom->add_entry(

		title    => $_title,

		link     => $_link,

		author   => { name => "Fayland Lam" },

		issued   => $_create_data,

		created  => $_create_data,

		modified => $_modified_data,

		content  => $_description,

	)

	or die;

}



$atom->save_file($Atom_file);



sub date2w3cdtf {

	my $data = shift;

	# the original data foramt like 2005-03-29 23:02:14 & it's a localtime

	# so we convert localtime to $time and got the gmtime



	my ($year, $mon, $mday, $hour, $min, $sec) = ($data =~ /^(\d )-(\d )-(\d )\s(\d ):(\d ):(\d )$/);

	$mon--;

	use Time::Local;

	my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);

	( $sec, $min, $hour, $mday, $mon, $year) = gmtime($time);

	$year  = 1900; $mon  ;



	# at last we return the w3c dtf

	my $timestring = sprintf( "M-d-dTd:d:dZ",

        $year, $mon, $mday, $hour, $min, $sec );

 	return ($timestring);

}


(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:PHP 对比 PERL  
下一篇:Win32 下 Apache 和 #!/usr/bin/perl
    评论加载中…
 推荐文章
     

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