Getting XML values in PHP with namespace prefix

This is a follow-on question to this: Getting XML attributes in PHP

I'm struggling to get element values with a namespace prefix. See this example from here:

<im:image height="53"></im:image>          
<im:image height="75"></im:image>

If I try and reference the element using $e->im:image I get the following error:

PHP Parse error:  syntax error, unexpected ':' 

$e->'im:image' gives me:

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' 

Any suggestions?

UPDATE: I've edited the question title which was misleading. It's actually element values with a namespace prefix (not attributes) that I'm interested in.


When using SimpleXML (as suggested by your previous question) you have to use the children() method to fetch all child elements of a certain namespace, same with attributes().


define('URI_RSS', '');
$feed = new SimpleXMLElement(getData());

foreach( $feed->entry as $entry ) {
    $attributes = $entry->id->attributes(URI_RSS);
    echo 'id[im:id]: ', $attributes['id'], "\n";

    $attributes = $entry->category->attributes(URI_RSS);
    echo 'category[im:id]: ', $attributes['id'], "\n";

    $imElements = $entry->children(URI_RSS);
    foreach( $imElements as $name=>$c ) {
        echo $name, '=', $c, "\n";

function getData() {
    return <<< eot
<?xml version="1.0" encoding="utf-8"?>

    <feed xmlns:im="" xmlns="" xml:lang="en">
        <id>someid</id><title>some title</title><updated>2012-09-10T02:12:06-07:00</updated><link rel="alternate" type="text/html" href="http://link1"/><link rel="self" href="http://link12"/><icon>http://iconlink1</icon><author><name>some name</name><uri>http://uri</uri></author><rights>all rights wronged</rights>


                    <id im:id="123456789" im:bundleId="">http://id</id>

                    <title>some title</title>

                    <summary>This is the summary.

A short text summarizing the complete article


                    <link rel="alternate" type="text/html" href="http://alternate"/>

                    <im:contentType term="Application" label="Application"/>

                    <category im:id="7890" term="example" scheme="http://scheme" label="example"/>

                    <link title="Preview" rel="enclosure" type="image/jpeg" href="http://preview" im:assetType="preview"><im:duration>0</im:duration></link>

                    <im:artist href="http://artist">sample artist</im:artist>

                    <im:price amount="0.99000" currency="USD">$0.99</im:price>

                    <im:image height="53">http://1.png</im:image>

                    <im:image height="75">http://2.png</im:image>

                    <im:image height="100">http://3.png</im:image>

                    <rights>all rights wronged</rights>

                    <im:releaseDate label="August 28, 2012">2012-08-28T00:00:00-07:00</im:releaseDate>

                    <content type="html">content



id[im:id]: 123456789
category[im:id]: 7890
artist=sample artist

Need Your Help

Counting time or cpu clicks in Cygwin

time cygwin cpu time.h

I can´t count the time of cpu processing in cygwin?Why is that?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.