note 107562 modified in simplexmlelement.getnamespaces by danbrown

From: Date: Tue, 19 Jun 2012 13:43:46 +0000
Subject: note 107562 modified in simplexmlelement.getnamespaces by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188689@lists.php.net to get a copy of this message
If the namespace is nested in the xml, then you will have to loop over the nodes.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;$xml = &#60;&#60;&#60;XML&#13;&#10;&#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62;&#13;&#10;  &#60;people xmlns:p=&#34;http://example.org/ns&#34; xmlns:t=&#34;http://example.org/test&#34;&#62;&#13;&#10;      &#60;items&#62;&#13;&#10;            &#60;title&#62;This is a test of namespaces and my patience&#60;/title&#62;&#13;&#10;            &#60;p:person id=&#34;1&#34;&#62;John Doe&#60;/p:person&#62;&#13;&#10;            &#60;p:person id=&#34;2&#34;&#62;Susie Q. Public&#60;/p:person&#62;&#13;&#10;            &#60;p:person id=&#34;1&#34;&#62;Fish Man&#60;/p:person&#62;&#13;&#10;      &#60;/items&#62;&#13;&#10;  &#60;/people&#62;&#13;&#10;XML;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;$sxe = new SimpleXMLElement($xml);&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;foreach ($sxe as $out_ns)&#13;&#10;{&#13;&#10;    $ns = $out_ns-&#62;getNamespaces(true);&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;    $child = $out_ns-&#62;children($ns[&#39;p&#39;]);&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;&#13;&#10;    foreach ($child as $out)&#13;&#10;    {&#13;&#10;        echo $out . &#34;&#60;br /&#62;&#34;;&#13;&#10;    }&#13;&#10;}&#13;&#10;?&#62; --was-- If the namespace is nested in the xml, then you will have to loop over the nodes. <?php $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test"> <items> <title>This is a test of namespaces and my patience</title> <p:person id="1">John Doe</p:person> <p:person id="2">Susie Q. Public</p:person> <p:person id="1">Fish Man</p:person> </items> </people> XML; $sxe = new SimpleXMLElement($xml); foreach ($sxe as $out_ns) { $ns = $out_ns->getNamespaces(true); $child = $out_ns->children($ns['p']); } print_r($child); foreach ($child as $out) { echo $out ."<br />"; } ?> http://php.net/manual/en/simplexmlelement.getnamespaces.php

« previous php.notes (#188689) next »