元素的顺序输出
Order output of elements
我为我写了这个 RSS reader,它实际上工作得很好,但不幸的是我不明白 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 显示那部分,紧接着它显示作为 link 工作的标题。但应该是相反的顺序。
这可能真的很愚蠢,但我看这段代码太久了,找不到问题所在。
如果有人能帮助我,我将非常高兴。
谢谢
锚标签 <a>
的回显语句是错误的。我将 $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/
我为我写了这个 RSS reader,它实际上工作得很好,但不幸的是我不明白 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 显示那部分,紧接着它显示作为 link 工作的标题。但应该是相反的顺序。
这可能真的很愚蠢,但我看这段代码太久了,找不到问题所在。
如果有人能帮助我,我将非常高兴。 谢谢
锚标签 <a>
的回显语句是错误的。我将 $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/