deezerd Posted December 8, 2006 Share Posted December 8, 2006 HiI'm using the SimpleXMLElement class to get some info of an rss feedThe thing is I get nothing about the info included in the namespaces (<media:title> <media:content> <media:credit>) when I writeprint_r($xml)For example in this code, what should I do to the info in the media node:[code]<media:player url="http://youtube.com/?v=hjlQfMECSdA"/> <media:thumbnail url="http://sjl-static10.sjl.youtube.com/vi/hjlQfMECSdA/2.jpg" width="120" height="90"/>[/code]thanks! Quote Link to comment https://forums.phpfreaks.com/topic/29936-get-namespaces-with-php/ Share on other sites More sharing options...
sKunKbad Posted May 31, 2007 Share Posted May 31, 2007 I need the same solution. Did you figure it out? Quote Link to comment https://forums.phpfreaks.com/topic/29936-get-namespaces-with-php/#findComment-265356 Share on other sites More sharing options...
sKunKbad Posted May 31, 2007 Share Posted May 31, 2007 This is from [url=http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/]http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/[/url], and was the only documented example I could find regarding parsing XML namespaces with php:SimpleXML and namespacesby Kevin YankThere’s a lot about SimpleXML, PHP5’s new API for accessing the contents of XML documents, in SitePoint’s recently-published book No Nonsense XML Web Development With PHP, but one thing it doesn’t cover is how to use SimpleXML with a document that makes use of XML Namespaces.Take this document, for example—a simplified RSS 1.0 feed:[code]<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel rdf:about="http://www.sitepoint.com/"> <title>SitePoint.com</title> <link>http://www.sitepoint.com/</link> <description>SitePoint is the natural place to go to grow your online business.</description> <items> <rdf:Seq> <rdf:li rdf:resource="http://www.sitepoint.com/article/take-command-ajax" /> </rdf:Seq> </items> </channel> <item rdf:about="http://www.sitepoint.com/article/take-command-ajax"> <title>Take Command with AJAX</title> <link>http://www.sitepoint.com/article/take-command-ajax</link> <description>Want to get a bang out of your AJAX artillery?</description> <dc:date>2005-10-14T04:00:00Z</dc:date> </item> </rdf:RDF>[/code]In PHP5, here’s how you might think to use SimpleXML’s API to get at the date of every item in the feed:[code=php:0]$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf'); foreach ($feed->item as $item) { echo $item->date; }[/code]But this won’t work, because the date element has a namespace prefix (<dc:date>), so it can’t be accessed by the usual means.Here’s the solution. First, check what the URI is for the namespace. In this case, the dc: prefix maps to the URI http://purl.org/dc/elements/1.1/:[code]<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">[/code]Then use the children method of the SimpleXML object, passing it that URI:[code=php:0]$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf'); foreach ($feed->item as $item) { $ns_dc = $item->children('http://purl.org/dc/elements/1.1/'); echo $ns_dc->date; }[/code]When you pass the namespace URI to the children method, you get a SimpleXML collection of the child elements belonging to that namespace. You can work with that collection the same way you would with any SimpleXML collection.You can use the attributes [url=http://www.php.net/manual/en/function.simplexml-element-attributes.php]http://www.php.net/manual/en/function.simplexml-element-attributes.php[/url] in the same way to obtain attributes with namespace prefixes. Quote Link to comment https://forums.phpfreaks.com/topic/29936-get-namespaces-with-php/#findComment-265913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.