@ 2014-01-30T13:46:41Z <?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">
<entry>
<yt:duration seconds="1870"/>
<yt:duration seconds="1875"/>
</entry>
</feed>';
$xml = new SimpleXMLElement($buffer);
echo "ibxml version: ", LIBXML_DOTTED_VERSION, "\n";
foreach ($xml->entry as $item)
{
//original comment: how to get seconds?
$namespaces = $item->getNameSpaces(true);
$yt = $item->children($namespaces['yt']);
var_dump($yt);
$seconds = $yt->duration->attributes();
echo $seconds['seconds'], "\n"; // original comment: but doesn't work.
}
echo "done. should read 1870 one time.\n";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.36 , 5.6.22 - 5.6.28 , 7.0.7 - 7.0.20 , 7.1.0 - 7.1.7 , 7.1.20 , 7.2.6 - 7.2.33 , 7.3.12 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 ibxml version: 2.9.4
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 7.2.0 ibxml version: 2.9.7
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 7.1.10 ibxml version: 2.9.5
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.5.0 - 5.5.35 , 5.6.0 - 5.6.21 , 7.0.0 - 7.0.6 ibxml version: 2.9.3
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.4.0 - 5.4.45 ibxml version: 2.9.2
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.3.25 - 5.3.29 ibxml version: 2.9.1
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.3.15 - 5.3.24 ibxml version: 2.8.0
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.2.1 - 5.2.17 , 5.3.0 - 5.3.14 ibxml version: 2.7.8
object(SimpleXMLElement)#5 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#7 (0) {
}
[1]=>
object(SimpleXMLElement)#8 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.1.3 - 5.1.6 , 5.2.0 ibxml version: 2.7.8
object(SimpleXMLElement)#6 (1) {
["duration"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#8 (0) {
}
[1]=>
object(SimpleXMLElement)#9 (0) {
}
}
}
1870
done. should read 1870 one time.
Output for 5.1.2 ibxml version: 2.7.8
object(SimpleXMLElement)#5 (0) {
}
1870
done. should read 1870 one time.
Output for 5.1.0 - 5.1.1 ibxml version: 2.7.8
Fatal error: Call to undefined method SimpleXMLElement::getNameSpaces() in /in/Kc9Ic on line 21
Process exited with code 255 . Output for 5.0.0 - 5.0.5 ibxml version:
Notice: Use of undefined constant LIBXML_DOTTED_VERSION - assumed 'LIBXML_DOTTED_VERSION' in /in/Kc9Ic on line 16
LIBXML_DOTTED_VERSION
Fatal error: Call to undefined method SimpleXMLElement::getNameSpaces() in /in/Kc9Ic on line 21
Process exited with code 255 . Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.9 Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/Kc9Ic on line 14
Process exited with code 255 . Output for 4.3.0 - 4.3.1 Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/Kc9Ic on line 14
preferences:dark mode live preview
270.72 ms | 401 KiB | 348 Q