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

Source: Internet
Author: User
Tags arrays closing tag count variables php script xml parser

This is the arrays with the each array element representing an
<item>//each outer array element was itself an associative array
With keys ("title", "link", "description")
$items = Array (),

Opening Tag Handler
function Elementbegin ($parser, $name, $attributes)
{
Global $currentTag, $flag,
$currentTag = $name,
Set flag if entering <channel> or <item> block
if ($name = = "ITEM")
{
$flag = 1,
}
else if ($name = = "CHANNEL")
{
$flag = 2,
}
}

Closing Tag Handler
function Elementend ($parser, $name)
{
Global $currentTag, $flag, $count,
$currentTag = "",

Set flag if exiting <channel> or <item> block
if ($name = = "ITEM")
{
$count + +,
$flag = 0,
}
else if ($name = = "CHANNEL")
{
$flag = 0,
}
}

Character Data Handler
function Characterdata ($parser, $data)
{
Global $currentTag, $flag, $items, $count, $channel,
$data = Trim (Htmlspecialchars ($data)),
if ($currentTag = = "TITLE" | | $currentTag = = "LINK" | |
$currentTag = =
"DESCRIPTION")
{
Add data to $channels [] or $items [] array
if ($flag = = 1)
{
$items [$count][strtolower ($CURRENTTAG)]. =
$data,
}
else if ($flag = = 2)
{
$channel [Strtolower ($currentTag)]. = $data,
}
}

}

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),
Xml_parser_set_option ($xp, Xml_option_skip_white, 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)),
}
}

Destroy parser
Xml_parser_free ($XP),

Now iterate through $items [] Array
and print each item as a table row
foreach ($items as $item)
{
echo "<tr><td><a href=". $item ["link"]. ">". $item ["title"].
"</a><br>". $item ["description"]. "</td></tr>",}

?>
</table>
</body>
The main difference from the previous paragraph was that the script created two arrays to hold the information extracted during the analysis. The $channel is a federated array (associative array) that holds the basic description of the channel being processed, and $items is a two-dimensional array containing information about the individual channel entries (channel Intems). Each element in the $items array is itself a federated array containing the Title,url and description keywords. The total number of elements in the $items array is the same as the total number of <item> blocks in the RDF document.
Also note the change in the $flag variable, which now holds two values, depending on whether the <channel></channel> block or <item></item> block is being processed. This is necessary because only in this way can the parser put information into the correct array.
Once the document is parsed, things are simple--traverse the $items array and print each entry (item) in a tabular format. The results of the journey are as follows:

 
7) Return to Class (Back to Class)

Since you have so much power, why should you limit yourself to just a single source of RDF? As I said earlier, most major sites often take snapshots of what they provide. In fact, it's fairly easy to insert all these different sources into your site. Let's see how it's done.
First, we modularize the code in the previous example. In this way, you don't have to rewrite the same code over and over again for each individual source. The simplified approach is to package it into a class, and then include this class in my PHP script.
The class code is as follows:
?
Class Rdfparser
{
//
Variables
//

Set up local variables to this class
var $currentTag = "",
var $flag = "",
var $count = 0,

This is a associative array of channel data with keys
("title", "link", "description")
var $channel = array (),

This is a array of arrays with each array element
Representing an <item>
Each outer array element was itself an associative array
With keys ("title", "link", "description")
var $items = array (),


//
Methods
//

Set the name of the RDF file to parse
This is usually a local file
You could set it to a remote file if your PHP build supports
URL fopen ()
function Setresource ($file)
{
$this->file = $file,
}


Parse the RDF file set with Setresource ()
This populates the $channel and $items arrays
function Parseresource ()
{
Create parser
$this->xp = Xml_parser_create (),

Set object reference
Xml_set_object ($this->xp, $this),

Set handlers and parser options
Xml_set_element_handler ($this->xp, "Elementbegin",
"Elementend"),
Xml_set_character_data_handler ($this->xp,
"Characterdata"),
Xml_parser_set_option ($this->xp,
Xml_option_case_folding, TRUE),
Xml_parser_set_option ($this->xp, Xml_option_skip_white,
TRUE),

Read XML file



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.