note 107562 modified in simplexmlelement.getnamespaces by danbrown
| From: | danbrown@php.net | 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. <?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']); Â
 foreach ($child as $out)   {     echo
$out . "<br />"; Â Â
} } ?>
--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