动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Cgi/Perl → 阅读文章

 如何创建 Atom

作者来源: 
阅读 1209 人次 , 2006-2-16 14:43:00 

简介

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( "%4d-%02d-%02dT%02d:%02d:%02dZ",
        $year, $mon, $mday, $hour, $min, $sec );
 	return ($timestring);
}
  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP 对比 PERL
· 下一篇:Win32 下 Apache 和 #!/usr/bin/perl
· CGI的安全(一)
· Perl教学 第六篇 模式匹配之二
· Perl教学 第十一篇 文件系统之五
· Perl教学 第一篇 概述
· 用Perl语言进行Socket编程之三


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077