4

I've been playing arround nearly all day for getting attributes of xml item which is is namespaces. Part of XML: ...

<item>
      <title>name</title>
      <link>link</link>
      <media:thumbnail url="url" height="133" width="200"  />
</item>

... What I managed to get is title and link with following script:

$z = new XMLReader;
$z->open($gLink);
$doc = new DOMDocument;
while ($z->read() && $z->name !== 'item');
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
$gTitle = $node->title;
$gLink = $node->link;
$gThumb = $node->children('media', true)->thumbnail->children();
print_r($gThumb);

After printing $gThumb I get:

SimpleXMLElement Object ( [@attributes] => Array ( [url] => url [height] => 133 [width] => 200 ) ) 

And what I need to get is url from attribute. I would be very pleased to get any help.

6
  • Bet what I can not modify xml because it is taken from some RSS Commented Apr 15, 2011 at 16:52
  • possible duplicate of How to get an attribute with SimpleXML? Commented Apr 15, 2011 at 16:56
  • print_r($gThumb->xpath('@url')); echo (string)$gThumb['url']; isn't working too Commented Apr 15, 2011 at 17:04
  • if echo (string) $gThumb['url']; isnt working, you are doing something else wrong. In that case, provide a reproducable example. Commented Apr 15, 2011 at 17:10
  • For the (string)$gThumb['url']; have you removed the ->children() from the end of $gThumb = $node->children('media', true)->thumbnail->children(); before doing this? Just trying to eliminate any of the more obvious errors. Commented Apr 16, 2011 at 20:43

1 Answer 1

3

If its a SimpleXMLElement then use

$foo = (string)$gThumb->attributes()->url
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.