当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SimpleXMLElement::getNameSpaces()用法及代码示例



定义和用法

XML 是一种 mark-up 语言,用于在网络上共享数据,XML 用于人类 read-able 和机器 read-able。 SimpleXMLElement 类表示 PHP 中的 XML 文档。

这个SimpleXMLElement::getNamespaces()函数检索并返回文档中使用的命名空间。

用法

SimpleXMLElement::getNamespaces([$recursive]);

参数

Sr.No 参数及说明
1

recursive (Optional)

这是一个布尔值,如果传递 TRUE 此函数返回父节点和子节点的命名空间。

返回值

此函数返回一个包含名称空间的数组。

PHP版本

这个函数最初是在 PHP 版本 5 中引入的,并且适用于所有后续版本。

示例

以下示例演示了 SimpleXMLElement::getNamespaces() 函数的用法。

<html>
   <head>
      <body>
         <?php
            $str="<Tutorial xmlns:t='http://example.org/ns' xmlns:test='http://demo.com/test'>
               <t:Name test:ns='a'>JavaFX</t:Name>
               <t:Pages test:ns='b'>535</t:Pages>
               <t:Author test:ns='c'>Krishna</t:Author>
               <t:Version test:ns='d'>11</t:Version>
            </Tutorial>"; 
            $xml = new SimpleXMLElement($str);
            $result = $xml->getNamespaces(TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html>

这将产生以下结果 -

array(2) { ["t"]=> string(21) "http://example.org/ns" ["test"]=> string(20) "http://demo.com/test" }

示例

以下是此函数的示例 -

<html>
   <head>
      <body>
         <?php
            $str="<Employee xmlns:contact='http://example.org/ns'>
               <Name>Ramu</Name>
               <Age>25</Age>
               <contact:City>Hyderabad</contact:City>
               <contact:Phone>9848022338</contact:Phone>
               <contact:email>test@gmail.com</contact:email>
            </Employee>"; 
            $xml = new SimpleXMLElement($str);
            $result = $xml->getNamespaces(TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html> 

这将产生以下输出 -

array(1) { ["contact"]=> string(21) "http://example.org/ns" }

相关用法


注:本文由纯净天空筛选整理自 PHP - SimpleXMLElement::getNameSpaces() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。