实战案例
综合实战:做一个 RSS 阅读器。RSS(Really Simple Syndication)是博客/新闻站的标准 XML 格式,解析它就是标准 XML 操作。
RSS 结构
rss 根 → channel 频道 → item 条目(title/link/description/pubDate)。拿到任意网站的 RSS 地址就能解析。
代码示例
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>示例博客</title>
<link>https://example.com</link>
<description>技术分享</description>
<item>
<title>PHP 入门教程</title>
<link>https://example.com/php</link>
<description>从零开始学 PHP</description>
<pubDate>Mon, 01 Sep 2025 10:00:00 GMT</pubDate>
</item>
<item>
<title>MySQL 优化技巧</title>
<link>https://example.com/mysql</link>
<description>索引和慢查询</description>
<pubDate>Fri, 05 Sep 2025 08:30:00 GMT</pubDate>
</item>
</channel>
</rss>
<!-- PHP 解析 RSS -->
<!--
$rss = simplexml_load_file("https://example.com/feed.xml");
foreach ($rss->channel->item as $item) {
echo "<a href='{$item->link}'>{$item->title}</a><br>";
}
-->