How to insert RDF content into a Web site in PHP (2)

Source: Internet
Author: User
Tags date insert object model php script resource xml parser xmlns

<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>
</items>
You can also place a <image> block in it so you can publish the URL of the channel flag.
So for meat, each <item> block in the RSS1.0 document describes a separate resource in more detail, including headings, URLs, and resource descriptions.
<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>
</items>
In this example, the,<item> block describes a separate article in the Ttrog "channel" and provides a description and caption, as well as a URL for this article. Content collectors can use URLs to create "backwards" links.

As you can see, the RSS1.0 file is quite intuitive and easy to create, either manually or programmatically. The above examples and explanations are only illustrative, and usually you can do more with RSS1.0 and RDF. You'd better look at the links provided at the end of the article to get more information. Before we do that, however, we'll spend a few more minutes discussing how to insert a RSS1.0 document into your own web site.

4) Fresh Meat
Since RSS is technically a well-formed XML document, it can be handled using standard XML programming techniques. There are two main technologies: SAX (The simple API for XML) and Dom (the Document Object Model).
The SAX parser traverses the entire XML document while it is working, and calls a specific function when it encounters a type-free tag. For example, call a specific function to handle a start tag, call another function to handle an end tag, and then call a function to handle the data between the two. The parser's job is simply to traverse the document sequentially. And the function it calls is responsible for handling the discovered markup. Once a tag is processed, the parser continues to parse the next element in the document, and the process repeats itself.
On the other hand, the DOM parser works by reading the entire XML document into memory and converting it into a hierarchical tree structure. It also provides APIs for accessing different tree nodes (and the content attached to the nodes). Recursive processing plus API functions enable developers to distinguish between different types of nodes (elements, attributes, character data, annotations, etc.), and make it possible to perform different actions depending on the node type and node depth of the document tree.
Sax and Dom parsers support almost every language, including your favorite--php. I'll use PHP's SAX parser to deal with RDF examples in this article. Of course, it's also easy to use a DOM parser.
Let's take a look at this simple example and write it down in the brain. Here's an RDF file I'm going to use, which is directly selected from http://www.freshmeat.net/:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
&LT;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/"
>
<channel rdf:about= "http://freshmeat.net/" >
<title>freshmeat.net</title>
<link>http://freshmeat.net/</link>
<description>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.</description>
<dc:language>en-us</dc:language>
<dc:subject>Technology</dc:subject>
<dc:publisher>freshmeat.net</dc:publisher>
<dc:creator>freshmeat.net contributors</dc:creator>
<dc:rights>copyright (c) 1997-2002 osdn</dc:rights>
<dc:date>2002-02-11T10:20+00:00</dc:date>
<items>
<rdf:Seq>
<rdf:li rdf:resource= "http://freshmeat.net/releases/69583/"/>
<rdf:li rdf:resource= "http://freshmeat.net/releases/69581/"/>

<!--and so on-->

</rdf:Seq>
</items>
<image rdf:resource= "Fmii-button.gif"/>
<textinput rdf:resource= "http://freshmeat.net/search/"/>
</channel>

<image rdf:about= "Fmii-button.gif" >
<title>freshmeat.net</title>
<url>http://freshmeat.net/img/fmII-button.gif</url>
<link>http://freshmeat.net/</link>
</image>

<item rdf:about= "http://freshmeat.net/releases/69583/" >
<title>sloop.splitter 0.2.1</title>
<link>http://freshmeat.net/releases/69583/</link>
<description>a Real time sound effects program.</description>
<dc:date>2002-02-11T04:52-06:00</dc:date>
</item>

<item rdf:about= "http://freshmeat.net/releases/69581/" >
<title>apacompile 1.9.9</title>
<link>http://freshmeat.net/releases/69581/</link>
<description>a full-featured Apache Compilation howto.</description>
<dc:date>2002-02-11T04:52-06:00</dc:date>
</item>

<!--and so on-->

</rdf:RDF>

The following is a PHP script that analyzes the document and displays the data:
<?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)),



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.