The previous example is just for illustration of the problem. If you really want to insert RDF content into a Web site, you need to do something a little better. So the previous script has been improved to add something to simplify the task of formatting RDF data.
<?php
XML file
$file = "HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF";
Set up some variables for use by the parser
$currentTag = "";
$flag = "";
$count = 0;
This is a associative array of channel data with keys ("title")
"Link",
"description")
$channel = Array ();
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 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.