对于给定的xml,
<?xml version="1.0" encoding="utf-8"?>
<College xmlns:A1="zool:"xmlns:A12="dym:">
<A1:name>Seattle</A1:name>
<A1:MiddleName>I</A1:MiddleName>
<A12:lastname>Roya Asbari</A12:lastname>
</College>
如何在迭代时获取每个节点的名称空间。
xml的格式每次都会有所不同,所以我正在寻找一种通用的方法。
答案 0 :(得分:1)
$xml = <<< XML
<College xmlns:A1="zool:" xmlns:A12="dym:">
<A1:name>Seattle</A1:name>
<A1:MiddleName>I</A1:MiddleName>
<A12:lastname>Roya Asbari</A12:lastname>
</College>
XML;
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXml($xml, LIBXML_NOERROR);
foreach($dom->documentElement->childNodes as $child)
{
printf(
"Name: %s - NS: %s - Prefix: %s\n",
$child->nodeName,
$child->namespaceURI,
$child->prefix
);
}
Name: A1:name - NS: zool: - Prefix: A1
Name: A1:MiddleName - NS: zool: - Prefix: A1
Name: A12:lastname - NS: dym: - Prefix: A12