Php For循环不显示XML文件中所有行的数据
我有一个XML文件,其结构如下:Php For循环不显示XML文件中所有行的数据,php,html,xml,Php,Html,Xml,我有一个XML文件,其结构如下: <item> <title></title> <link></link> <description></description> <content:encode></content:encode> <pubDate></pubDate> <author></author> <
<item>
<title></title>
<link></link>
<description></description>
<content:encode></content:encode>
<pubDate></pubDate>
<author></author>
<guid></guid>
<dc:date></dc:date>
</item>
我正在显示上面的特定数据
loadXML($importBuffer);
$xpath=newdomxpath($dom);
$items=$xpath->query(“//item”);
$results=parse($items);
$col=$dom->getElementsByTagName('item');
$i=0;
foreach($col作为$item){
$blogTitle=$item->childNodes[1]->nodeValue;
$blogImage=$item->childNodes[5]->nodeValue;
$blogDate=$item->childNodes[9]->nodeValue;
$blogAuthor=$item->childNodes[11]->nodeValue;
如果(++$i>4)中断;
?>
您对$item
的子节点的访问是危险的
这对像这样的东西会有用的
<item>
<title>Title</title>
<link>Link</link>
<description>Desc</description>
<content:encode>Encode</content:encode>
<pubDate>Date</pubDate>
<author>Author</author>
<guid>Guid</guid>
<dc:date>Date</dc:date>
</item>
或者使用dom查询更好
$blogTitle=$item->getElementsByTagName('title')->item(0)->nodeValue;
$blogImage=$item->getElementsByTagName('description')->item(0)->nodeValue;
$blogDate=$item->getElementsByTagName('pubDate')->item(0)->nodeValue;
$blogAuthor=$item->getElementsByTagName('author')->item(0)->nodeValue;
项子节点的索引可能不同。它们可以具有不同的顺序、其他节点(包括注释)或只是一些换行符
默认情况下,节点之间的空白将被解析为文本节点。这允许库保存相同的XML。您可以设置DOMDocument::preserveWhiteSpace
属性来避免它
您开始使用Xpath,但后来忽略了结果。Xpath表达式允许您更有针对性地访问XML节点
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
// fetch all item nodes and limit to the first 4
$items = $xpath->evaluate("(//item)[position() < 5]");
foreach ($items as $index => $item) {
// cast the first title child node to string - empty if not found
$blogTitle = $xpath->evaluate('string(title)', $item);
// ...
}
$document=新的DOMDocument();
$document->loadXML($xml);
$xpath=newdomxpath($document);
//获取所有项目节点并限制为前4个
$items=$xpath->evaluate((//item)[position()<5]”;
foreach($index=>$item的项目){
//将第一个标题子节点强制转换为字符串-如果未找到,则为空
$blogTitle=$xpath->evaluate('string(title)',$item);
// ...
}
这似乎不是您真正的代码。条件
将导致只显示一项。这查询选择器
?这是从哪里来的?感谢您解释问题的原因。但是正如RamRaider提到的,查询选择器从哪里来?PHP的DOMDocument不支持它?@Freddy我很抱歉。一直处于JS模式。我现在使用getElementsByTagName
更改了代码。这是untested@yunzen-不用担心,谢谢你,它现在正在工作(而且是一个更具体的方法)。再次感谢:)
$blogTitle=$item->getElementsByTagName('title')->item(0)->nodeValue;
$blogImage=$item->getElementsByTagName('description')->item(0)->nodeValue;
$blogDate=$item->getElementsByTagName('pubDate')->item(0)->nodeValue;
$blogAuthor=$item->getElementsByTagName('author')->item(0)->nodeValue;
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
// fetch all item nodes and limit to the first 4
$items = $xpath->evaluate("(//item)[position() < 5]");
foreach ($items as $index => $item) {
// cast the first title child node to string - empty if not found
$blogTitle = $xpath->evaluate('string(title)', $item);
// ...
}