PHP XML echoing values contained in a node
I have a XML doc , with 3 article nodes in it , each Article contains : Title Image Link
I need to be able to get the Title/Image/Link values out of the node.
$query = $xpath->query('//section/article');
foreach($query as $currentArticle => $artContents):
print_r( $artContents->title);
endforeach
; This doesn't work for me, I can use ->NodeName and nodeValue , but they dont drill down enough , just display 'article' or the whole contents respectively.
To explain more:
My XML is
<article>
<title>Title </title>
<link>http:// </link>
<img>image src </img>
</article>
and the output I require is :
Title->Title
Link->http://
etc.
Update to explain :
foreach($query as $articles):
foreach($articles->childNodes as $childNode) {
if ($childNode->nodeType === XML_ELEMENT_NODE) {
$stored = $childNode->nodeValue;
array_push( $availAds, $stored );
}
}
endforeach;
is what I currently have thanks to Gordon . This though makes my array look like:
//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) "开发者_Go百科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"
I obviously need my array to be consistent, but cannot work out how to do that. Thanks for your patience.
Bob
I'm still not sure what the problem is, but are you looking for
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);
}
}
}
This will iterate over all the DOMElement nodes that are direct children of the $article nodes returned in the query and print them in this format "nodename->nodevalue" and a newline.
精彩评论