如何用PHP把RDF内容插入Web站点之中(二)
web|插入|站点
既然从技术上讲,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/ :
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
http://freshmeat.net/
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.
http://freshmeat.net/
http://freshmeat.net/releases/69583/
http://freshmeat.net/releases/69581/
下面是分析这一文档并显示其中数据的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)));
}
}
// destroy parser
xml_parser_free($xp);
// opening tag handler
function elementBegin($parser, $name, $attributes)
{
global $currentTag, $flag;
// export the name of the current tag to the global scope
$currentTag = $name;
// if within an item block, set a flag
if ($name == "ITEM")
{
$flag = 1;
}
}
// closing tag handler
function elementEnd($parser, $name)
{
global $currentTag, $flag;
$currentTag = "";
// if exiting an item block, print a line and reset the flag
if ($name == "ITEM")
{
echo "
";
$flag = 0;
}
}
// character data handler
function characterData($parser, $data)
{
global $currentTag, $flag;
// if within an item block, print item data
if (($currentTag == "TITLE" || $currentTag == "LINK" ||
$currentTag ==
"DESCRIPTION") && $flag == 1)
{
echo "$currentTag: $data
";
}
}
?>
看不明白? 别着急,后面将会作出解释。
捕获旗标
这段脚本首先要做的是设定一些全局变量:
// XML file
$file = "fm-releases.rdf";
// set up some variables for use by the parser
$currentTag = "";
$flag = "";
$currentTag变量保存是分析器当前处理的元素的名称——你很快就会看到为什么需要它。
因为我的最终目的是显示频道中的每一个单独的条目(item),并且带有链结。另外还要知道分析器什么时候退出了
$flag变量将用于判断分析器是在
下一步要做的是初始化SAX分析器,并开始分析RSS文档。
// 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)));
}
}
// destroy parser
xml_parser_free($xp);
这段代码简单明了,其中的注释已经解释的足够清楚了。xml_parser_create()函数建立一个分析器实例,并将之赋给句柄$xp。接着再创建回调函数处理开标记和闭标记,以及二者之间的字符数据。最后,xml_parse()函数联合多次fread()调用,读取RDF文件并分析它。
在文档中,每次遇到开标记,开标记处理器elementBegin()就会被调用。
// opening tag handler
function elementBegin($parser, $name, $attributes)
{
global $currentTag, $flag;
// export the name of the current tag to the global scope
$currentTag = $name;
// if within an item block, set a flag
if ($name == "ITEM")
{
$flag = 1;
}
}
这个函数以当前标记的名称和属性作为起参数。标记名称被赋值给全局变量$currentTag。如果,这个开标记是
同样,如果遇到闭标记,那么闭标记处理器elementEnd()将被调用。
// closing tag handler
function elementEnd($parser, $name)
{
global $currentTag, $flag;
$currentTag = "";
// if exiting an item block, print a line and reset the flag
if ($name == "ITEM")
{
echo "
";
$flag = 0;
}
}
闭标记处理函数也是以标记名称作为其参数。如果是遇到的是一个为
那么,如何处理标记之间的字符数据呢? 这才是我们的兴趣所在。先向字符数据处理器characterData()打个招呼吧。
// character data handler
function characterData($parser, $data)
{
global $currentTag, $flag;
// if within an item block, print item data
if (($currentTag == "TITLE" || $currentTag == "LINK" ||
$currentTag ==
"DESCRIPTION") && $flag == 1)
{
echo "$currentTag: $data
";
}
}
现在你可以看一下传给这个函数的参数,你会发现它只接收了开标记和闭标记之间的数据,而根本不知道分析器当前正在处理哪个标记。而这正事我们一开始就引入全局变量$currentTag的原因。
如果$flag变量的值为1,也就是说如果分析器当前处于
。
整个RDF文档就是以这种顺序方式处理,每发现一个

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Everyone is looking forward to today's Windows 1123H2 release. In fact, Microsoft has just launched updates to the release preview, which is the closest channel before the official release stage. Known as Build 22631, Microsoft said they are rolling out the new rebranded chat app, phone link, and play together widgets that have been tested on other internal channels over the past few months. "This new update will have the same servicing branch and codebase as Windows 11 version 22H2 and will be cumulative with all newly announced features, including Copilot in Windows (preview)," Microsoft promises. Redmond officials further

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求
