3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * How to get attribute of node with namespace using SimpleXML? * * @link http://stackoverflow.com/q/6576773/367456 */ $buffer = '<ns:feed xmlns:ns="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <ns:entry> <yt:duration seconds="1870"/> </entry> </feed>'; $xml = new SimpleXMLElement($buffer); print_r($xml); echo "ibxml version: ", LIBXML_DOTTED_VERSION, "\n"; echo $xml->entry; foreach ($xml->entry as $item) { //original comment: how to get seconds? $namespaces = $item->getNameSpaces(true); $yt = $item->children($namespaces['yt']); $seconds = $yt->duration->attributes(); echo $seconds['seconds'], "\n"; // original comment: but doesn't work. } echo "done. should read 1870 one time.\n";
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: SimpleXMLElement::__construct(): Entity: line 4: parser error : Opening and ending tag mismatch: entry line 2 and entry in /in/69XmE on line 13 Warning: SimpleXMLElement::__construct(): </entry> in /in/69XmE on line 13 Warning: SimpleXMLElement::__construct(): ^ in /in/69XmE on line 13 Warning: SimpleXMLElement::__construct(): Entity: line 5: parser error : Opening and ending tag mismatch: feed line 1 and feed in /in/69XmE on line 13 Warning: SimpleXMLElement::__construct(): </feed> in /in/69XmE on line 13 Warning: SimpleXMLElement::__construct(): ^ in /in/69XmE on line 13 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/69XmE:13 Stack trace: #0 /in/69XmE(13): SimpleXMLElement->__construct('<ns:feed xmlns:...') #1 {main} thrown in /in/69XmE on line 13
Process exited with code 255.

preferences:
191.35 ms | 404 KiB | 198 Q