PHP getDocNamespaces() 函数

定义和用法

getDocNamespaces() 函数从 SimpleXMLElement 对象返回在 XML 文档中声明的命名空间。

如果成功,该函数返回包含命名空间名称(带有关联的 URL)的数组。如果失败,则返回 false。

语法

class SimpleXMLElement
{
string getDocNamespaces(recursive)
}
参数 描述
recursive 可选。规定是否返回父子节点中的所有命名空间。默认是 false。

例子

XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note xmlns:b="/example/">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<b:body>Don't forget the meeting!</b:body>
</note>

PHP 代码:

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  }

print_r($xml->getDocNamespaces());
?>

输出类似:

Array
(
[b] => /example/
)