我为我写了这个RSS阅读器,它实际上工作得非常好,但遗憾的是我不了解PHP如何输出。有人可以帮我吗?
我的PHP代码:
$reader = new XMLReader();
$reader->open("articles.xml");
while ($reader->read())
{
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title")
echo "<h1>Pøehled aktuálních zpráv ze serveru
<a href='" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "'>" .
$title . "</a></h1>";
// obsluha názvu kanálu
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link")
$title = htmlspecialchars($reader->readString());
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "article")
{
echo "<dl>";
while ($reader->read())
{
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link")
echo "<dt><a href='" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "'>" .
$title . "</a></dt>";
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title")
$title = htmlspecialchars($reader->readString());
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "description")
echo "<dd>" . htmlspecialchars($reader->readString()) . "</dd>";
}
echo "</dl>";
}
}
这是输出 1.它使用desc显示该部分,然后显示作为链接的标题。但它应该是相反的顺序。
这可能是非常愚蠢的事情,但我对这段代码的关注时间太长而无法找到问题。
如果有人可以帮助我,我会很高兴。 感谢
答案 0 :(得分:1)
锚标记<a>
的echo语句错误。我将$title
与htmlspecialchars($reader->readString(), ENT_QUOTES)
交换。
这应该是你想要的:
$reader = new XMLReader();
$reader->open("http://blog.omer.london/feed/");
$title = '';
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title") {
echo "<h1>Pøehled aktuálních zpráv ze serveru
<a href='" . $title . "'>" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "</a></h1>";
}
// obsluha názvu kanálu
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link") {
$title = htmlspecialchars($reader->readString());
} elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "article") {
echo "<dl>";
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link") {
echo "<dt><a href='" . $title . "'>" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "</a></dt>";
} elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title") {
$title = htmlspecialchars($reader->readString());
} elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "description") {
echo "<dd>" . htmlspecialchars($reader->readString()) . "</dd>";
}
}
echo "</dl>";
}
}
以下是HTML输出:http://jsfiddle.net/0qt773eb/