3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * How to get attribute of node with namespace using SimpleXML? * * @link http://stackoverflow.com/q/6576773/367456 */ $buffer = '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:x-trumba="http://schemas.trumba.com/rss/x-trumba"> <entry> <yt:duration seconds="105">test</yt:duration> <yt:duration seconds="1870">test</yt:duration> <x-trumba:customfield name="Event Type" id="21" type="number">Sound Publishing Event</x-trumba:customfield> <x-trumba:customfield name="Category" id="25674" type="text">Family</x-trumba:customfield> </entry> </feed>'; $xml = new SimpleXMLElement($buffer); foreach ($xml->entry as $item) { //original comment: how to get seconds? $namespaces = $item->getNameSpaces(true); $yt = $item->children($namespaces['yt']); $trumba = $item->children($namespaces['x-trumba']); $seconds = $yt->duration->attributes(); echo $seconds['seconds'], "\n"; // original comment: but doesn't work. $numofItems = $trumba->customfield->count(); foreach ($item->children('x-trumba', TRUE) as $child) { //echo $child->asXML(); //echo $child->attributes('id')); echo $child->attributes('x-trumba', TRUE)->Name; } foreach ($trumba->customfield as $custom) { $name = $trumba->customfield->attributes(); // var_dump($custom); } }
Output for 5.6.0 - 7.3.0rc3
105