Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am pulling in an XML feed from a blogger blog and need to parse it using PHP. The data looks something like this, once I have used the simplexml_load_file() function:

["entry"]=> array(25) {["link"]=> array(5) {[4]=> object(SimpleXMLElement)#571 (1) {["@attributes"]=> array(4) {["rel"]=> string(9) "alternate"

When I try to target rel like so:

echo $xmldata[0]->entry[0]->link[4]->@attributes['rel'];

and

echo $xmldata[0]->entry[0]->link[4]->[@attributes]->rel;

I get the following errors:

Parse error: syntax error, unexpected '@', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

and

Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

respectively.

Any ideas?

share|improve this question
    
probably you need quotes echo $xmldata[0]->entry[0]->link[4]->["@attributes"]->rel; –  danneth Aug 15 at 12:41

3 Answers 3

You need to wrap @attributes in curly braces and in '' quotes.

echo $xmldata[0]->entry[0]->link[4]->{'@attributes'}['rel']
share|improve this answer
    
+1, I forgot the single quote. –  Ende Neu Aug 15 at 12:52

Try like this:

echo $xmldata[0]->entry[0]->link[4]['rel'];

The Basic SimpleXML usage documentation (example #5) shows how an element's attributes can be accessed just like an associative array as in the above.

share|improve this answer
    
Why the downvote? –  Robby Cornelissen Aug 15 at 12:46

Since @ is a keyword you need to wrap the identifier into curly brackets:

echo $xmldata[0]
  ->entry[0]
  ->link[4]
  ->{'@attributes'}['rel'];
share|improve this answer
    
I'm sorry, I'm a little bit confused here. Could you briefly explain why my answer is considered inferior or not useful? Trying to learn... –  Robby Cornelissen Aug 15 at 12:55
    
Well you should ask the downvoter, anyway, I suppose it's wrong because at link[4] there's an object, this object has a property called @attributes which is an array that contains rel, the problem is that being @ a keyword you can't access it so ->@attributes, for this you need to have brackets and single quotes around the identifier. Your answer bypass the @attributes identifier, also if there was a property called rel you would have used link[4]->rel since at link[4] there's an object. –  Ende Neu Aug 15 at 12:59
    
OK. Thanks for answering. According to the Basic SimpleXML documentation and my own tests, index-based access works fine for attributes though. –  Robby Cornelissen Aug 15 at 13:02
    
Interesting, I didn't know that, although there's no xml example with attributes in the documentation, at least I don't see it. –  Ende Neu Aug 15 at 13:07
    
Example #5 Using attributes –  Robby Cornelissen Aug 15 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.