如果命名空間嵌套在 XML 中,則您必須迴圈遍歷節點。
<?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>這是一個命名空間和我的耐心的測試</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 />";
}
}
?>