您好,欢迎访问一九零五行业门户网

如何用PHP把RDF内容插入Web站点之中(2)

li
rdf:resource=http://www.melonfire.com/community/columns/trog/article.ph
p?id
=71 />
li
rdf:resource=http://www.melonfire.com/community/columns/trog/article.ph
p?id
=62 />
rdf:seq>
还可以在其中放置一个区块,这样你就可以发布频道标志的url。
所以为了肉,rss1.0文档中的每一个区块都更详细地描述一个单独的资源,包括标题,url和资源描述。
items>
rdf:seq>
li
rdf:resource=http://www.melonfire.com/community/columns/trog/article.ph
p?id
=100 />
li
rdf:resource=http://www.melonfire.com/community/columns/trog/article.ph
p?id
=71 />
li
rdf:resource=http://www.melonfire.com/community/columns/trog/article.ph
p?id
=62 />
rdf:seq>
在这个例子里,区块描述了ttrog“频道”中单独的一篇文章,并为这篇文章提供了描述和标题,以及url。内容收集者可以利用url创建“向后”链接。
你看得到,rss1.0文件相当地直观明了,不管是手工,还是通过编程,都非常容易创建。上面的例子和解释仅仅是说明性质的,通常,你可以用rss1.0和rdf做更多的事情。你最好看一下文章末尾提供的链接,以获取更多的信息。不过在这之前,我们再花几分钟讨论一如何将rss1.0文档插入到你自己的web站点之中。
4)鲜肉
既然从技术上讲,rss是结构良好的xml文档,所以可以用标准的xml编程技术来处理它。主要有两种技术:sax(the simple api for xml)和dom(the document object model)。
sax分析器工作时遍历整个xml文档,在遇到不用类型的标记时调用特定的函数。比如,调用特定函数处理一个开始标记,调用另一个函数处理一个结束标记,再调用一个函数处理两者之间的数据。分析器的职责仅仅是顺序遍历这个文档。而它所调用的函数负责处理发现的标记。一旦一个标记被处理完毕,分析器继续分析文档中的下一个元素,这一过程不断重复。
另一方面,dom分析器工作是把整个xml文档读进内存当中,并将之转换成一种分层的树型结构。而且为访问不同的树结点(以及结点所附的内容)提供了api。递归处理方式加上api函数使得开发者能够区分不同类型的结点(元素,属性,字符数据,注释等),同时根据文档树的结点类型和结点深度,使得执行不同的动作成为可能。
sax和dom分析器几乎支持每一种语言,包括你我的最爱——php。我将在这篇文章中利用php的sax分析器处理rdf的例子。 当然,使用dom分析器也同样很容易。
让我们看这个简单的例子,把它记在脑海里。下面是一个我将要使用的rdf文件,这个文件直接选自http://www.freshmeat.net/ :
rdf:rdf xmlns:rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns#
xmlns=http://purl.org/rss/1.0/
xmlns:dc=http://purl.org/dc/elements/1.1/
>
rdf:about=http://freshmeat.net/>
freshmeat.net
http://freshmeat.net/
freshmeat.net maintains the web''s largest index of unix
and cross-platform open source software. thousands of applications are
meticulously cataloged in the freshmeat.net database, and links to new
code are added daily.
dc:language>en-usdc:language>
dc:subject>technologydc:subject>
dc:publisher>freshmeat.net
dc:creator>freshmeat.net contributorsdc:creator>
dc:rights>copyright (c) 1997-2002 osdndc:rights>
dc:date>2002-02-11t10:20+00:00dc:date>
rdf:seq>
rdf:li rdf:resource=http://freshmeat.net/releases/69583/ />
rdf:li rdf:resource=http://freshmeat.net/releases/69581/ />
rdf:seq>
rdf:resource=http://freshmeat.net/img/fmii-button.gif />
textinput rdf:resource=http://freshmeat.net/search/ />
rdf:about=http://freshmeat.net/img/fmii-button.gif>
freshmeat.net
http://freshmeat.net/img/fmii-button.gif
http://freshmeat.net/
rdf:about=http://freshmeat.net/releases/69583/>
sloop.splitter 0.2.1
http://freshmeat.net/releases/69583/
a real time sound effects program.
dc:date>2002-02-11t04:52-06:00dc:date>
rdf:about=http://freshmeat.net/releases/69581/>
apacompile 1.9.9
http://freshmeat.net/releases/69581/
a full-featured apache compilation howto.
dc:date>2002-02-11t04:52-06:00dc:date>
rdf:rdf>
下面是分析这一文档并显示其中数据的php脚本:
php
// xml file
$file = fm-releases.rdf,
// set up some variables for use by the parser
$currenttag = ,
$flag = ,
// create parser
$xp = xml_parser_create(),
// set element handler
xml_set_element_handler($xp, elementbegin, elementend),
xml_set_character_data_handler($xp, characterdata),
xml_parser_set_option($xp, xml_option_case_folding, true),
// read xml file
if (!($fp = fopen($file, r)))
{
die(could not read $file),
}
// parse data
while ($xml = fread($fp, 4096))
{
if (!xml_parse($xp, $xml, feof($fp)))
{
die(xml parser error: .
xml_error_string(xml_get_error_code($xp))),
其它类似信息

推荐信息