note 110740 deleted from function.simplexml-load-file by danbrown

From: Date: Thu, 06 Dec 2012 14:12:44 +0000
Subject: note 110740 deleted from function.simplexml-load-file by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192301@lists.php.net to get a copy of this message
Note Submitter: uramihsayibok, gmail, com ---- kannan's note about how colons seem to be special characters in element names, and using hyphens instead (renaming <node:number> to <node-number>), is because of XML namespaces. <?php $xml = simplexml_load_string(" <root xmlns:node='uri:foo'> <node:number>1538-7445</node:number> </root>"); // you might think $xml->node:number would work. no, that's not valid PHP syntax // ah right, of course. so how about echo $xml->{"node:number"}; // nope // (the syntactically-valid way of writing the same thing) // will not work because there is no element named "node:number" // I'll say that again: there is no element named "node:number" // what you want is the element named "number" *in the node namespace* echo $xml->number; // nope // that looks for "number" in the default (empty) namespace // use SimpleXMLElement::children() to switch namespaces echo $xml->children("node", true)->number; // works by the namespace name echo $xml->children("uri:foo")->number; // works by the namespace URI ?>

« previous php.notes (#192301) next »