How do I return all items labeled guid
from an XML document? For example:
[guid] => http://www.motorauthority.com/blog/1060293_aston-martin-v12-zagato-2011-concorso-deleganza-villa-deste
Here is my code:
$source = 'http://feeds.feedburner.com/motorauthority2?format=xml';
$dom = new DOMDocument();
@$dom->loadHTMLFile($source);
$xml = simplexml_import_dom($dom);
$blog = $xml->xpath("//channel");
print_r($blog);
Here is the output of print_r
:
Array
(
[0] => SimpleXMLElement Object
(
[language] => en
[title] => High Gear Media Network Feed
[description] => Latest news, reviews, and more from around the High Gear Media network of sites
[image] => SimpleXMLElement Object
(
[开发者_C百科link] => SimpleXMLElement Object
(
)
[url] => http://www.motorauthority.com/images/logo-footer.jpg
[title] => MotorAuthority
)
[lastbuilddate] => Fri, 20 May 2011 04:55:17 -0400
[generator] => High Gear Media
[link] => Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[atom10] => http://www.w3.org/2005/Atom
[rel] => self
[type] => application/rss+xml
[href] => http://feeds.feedburner.com/MotorAuthority2
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[atom10] => http://www.w3.org/2005/Atom
[rel] => hub
[href] => http://pubsubhubbub.appspot.com/
)
)
)
[info] => SimpleXMLElement Object
(
[@attributes] => Array
(
[uri] => motorauthority2
)
)
[explicit] => no
[subtitle] => Latest news, reviews, and more from around the High Gear Media network of sites
[meta] => SimpleXMLElement Object
(
[@attributes] => Array
(
[xhtml] => http://www.w3.org/1999/xhtml
[name] => robots
[content] => noindex
)
)
[feedflare] => Array
(
[0] => Subscribe with My Yahoo!
[1] => Subscribe with NewsGator
[2] => Subscribe with My AOL
[3] => Subscribe with Bloglines
[4] => Subscribe with Netvibes
[5] => Subscribe with Google
[6] => Subscribe with Pageflakes
[7] => Subscribe with Plusmo
[8] => Subscribe with The Free Dictionary
[9] => Subscribe with Bitty Browser
[10] => Subscribe with NewsAlloy
[11] => Subscribe with Live.com
[12] => Subscribe with Excite MIX
[13] => Subscribe with Attensa for Outlook
[14] => Subscribe with Webwag
[15] => Subscribe with Podcast Ready
[16] => Subscribe with Flurry
[17] => Subscribe with Wikio
[18] => Subscribe with Daily Rotation
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => Aston Martin V12 Zagato: 2011 Concorso d'Eleganza Villa d'Este
[description] => The weekend is finally upon us and that means the 2011 Concorso d’Eleganza Villa d’Este is kicking off, along with all that comes with it. Yesterday we saw a sneak peek at the new Ferrari Superamerica 45, built under the automaker’s Special Projects division and destined for a home in New York. Today we have another coachbuilt...]]>
[pubdate] => Fri, 20 May 2011 04:55:17 -0400
[link] => SimpleXMLElement Object
(
)
[guid] => http://www.motorauthority.com/blog/1060293_aston-martin-v12-zagato-2011-concorso-deleganza-villa-deste
The XPath expression you need is
/rss/channel/item/guid
Note that simplexml_load_file()
can handle a URI. There is no need for going though DOMDocument
.
You've already got the XML converted into a PHP array, so it should just be a case of recursively scanning the array for the key you're looking for.
Something like this should do it:
<?php
function array_key_search_recursive($array,$keytofind) {
$output = array();
foreach($array as $key=>$value) {
if($key == $keytofind) {
$output[] = $value;
} elseif(is_array($value)) {
$output = array_merge($output, array_key_search_recursive($value,$keytofind));
}
}
return $output;
}
?>
(note I haven't tested that code yet; I just knocked it together now, so it may have bugs)
[EDIT] First thing I notice after posting this answer is that you actually haven't converted it to an array; it's still an object (it's just print_r
formatting it like an array). The above should still work, but you'd need to work with it as an object; possibly the easist way is just to cast the $array
and $value
as type array in the foreach
loop.
精彩评论