【发布时间】:2011-03-24 12:01:01
【问题描述】:
我有一个 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>
我需要的输出是:
标题->标题
链接->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"
我显然需要我的数组保持一致,但不知道如何做到这一点。谢谢你的耐心。
鲍勃
【问题讨论】: