php/simplexml and namespace in rdf

Using SimpleXml extension to parse rdf file.

Here can be the following problem. For example we have next rdf file:




my desc 
March, 10 


and this php code to parse xml:

$rdf = simplexml_load_file($path2Rdf);

foreach ($rdf->item as $item)
{
    echo $item->date;
}

However this code doesn’t work, because element “date” has a namespace prefix of “dc”, so the script doesn’t have an access to this namespace.

In order to have an access to date element we have to define the namespace in xml file


in php we have to use children method to get a collection of elements which belong to “dc” namespace:

$dc = $item->children('http://purl.org/dc/elements/1.1/');
echo $dc->date;

This entry was posted on Wednesday, April 4th, 2007 at 12:07 PM and is filed under php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.