我有一个XML文档,其中有3个文章节点,每篇文章都包含: 标题 图片 链路
我需要能够从节点中获取Title / Image / Link值。
$query = $xpath->query('//section/article');
foreach($query as $currentArticle => $artContents):
print_r( $artContents->title);
endforeach
这对我不起作用,我可以使用 - > NodeName和nodeValue,但是它们没有足够深入,只是分别显示“文章”或整个内容。
要解释更多:
我的XML是
<article>
<title>Title </title>
<link>http:// </link>
<img>image src </img>
</article>
我需要的输出是:
标题 - &GT;名称
链结&GT; HTTP://
等
更新解释:
foreach($query as $articles):
foreach($articles->childNodes as $childNode) {
if ($childNode->nodeType === XML_ELEMENT_NODE) {
$stored = $childNode->nodeValue;
array_push( $availAds, $stored );
}
}
endforeach;
感谢Gordon,我现在拥有的是。这虽然使我的数组看起来像://previous values:
}
["1300884672_071.jpg"]=>
array(3) {
["image"]=>
string(30) "1300884672_071.jpg"
["title"]=>
string(6) "secind title"
["link"]=>
string(10) "grtgrtgrtg"
}
["1300884618_071.jpg"]=>
array(3) {
["image"]=>
string(30) "1300884618_071.jpg"
["title"]=>
string(5) "first title"
["link"]=>
string(10) "http://www.google.com"
}
//updated values that
[0]=>
string(6) "My Title"
[1]=>
string(89) "/1300961550.jpg"
[2]=>
string(22) "rtherherhgerg thursada"
[3]=>
string(20) "custome 222222222222"
我显然需要我的数组保持一致,但无法解决如何做到这一点。谢谢你的耐心。
鲍勃答案 0 :(得分:1)
我仍然不确定问题是什么,但你在寻找
foreach($query as $articles):
foreach($articles->childNodes as $childNode) {
if ($childNode->nodeType === XML_ELEMENT_NODE) {
printf("%s->%s%s", $childNode->nodeName, $childNode->nodeValue, PHP_EOL);
}
}
}
这将迭代所有DOMElement节点,这些节点是查询中返回的$ articles节点的直接子节点,并以“nodename-&gt; nodevalue”格式和换行符打印它们。