Query XML file with PHP to return certain bookmarks
I have an XML file full of bookmarks from Google Bookmarks. (File: http://gist.github.com/324844) I want to pull the bookmark based on this path: xml_api_reply->bookmarks-bookmark->labels-开发者_Go百科>label.
So, my question is How can I use SimpleXML to grab the bookmarks that have the label Inspiration? Some bookmarks may have more than one bookmark.
EDIT: The file listed above is just a sample.
You'll have to use XPath for that.
The nodes you want:
/xml_api_reply/bookmarks/bookmark
The filter you want to apply:
[labels/label = "Inspiration"]
Gives you the following XPath query:
/xml_api_reply/bookmarks/bookmark[labels/label = "Inspiration"]
Used in a script:
$xml_api_reply = simplexml_load_file('http://gist.github.com/raw/324844/e4b1e05118b09c61c2a5b8b9a3bab92b895de07c/GoogleBookmarksXMLOutput');
foreach ($xml_api_reply->xpath('/xml_api_reply/bookmarks/bookmark[labels/label = "Inspiration"]') as $bookmark)
{
// ...
echo $bookmark->asXML();
}
Alternatively, you could also use the shorter //bookmark[labels/label = "Inspiration"]
-- see that XPath tutorial
精彩评论