PHP - SimpleXMLElement::getDocNamespaces() 函式



定義和用法

XML 是一種標記語言,用於在網路上共享資料,XML 既可供人類閱讀,也可供機器讀取。SimpleXMLElement 類在 PHP 中表示 XML 文件。

SimpleXMLElement::getDocNamespaces() 函式檢索並返回文件中宣告的名稱空間。

語法

SimpleXMLElement::getDocNamespaces([$recursive, $from_root]);

引數

序號 引數 & 說明
1

recursive (可選)

這是一個布林值,如果傳遞 TRUE,則此函式返回父節點和子節點的名稱空間。

2

from_root (可選)

這是一個布林值,如果傳遞 TRUE,則此函式檢查子節點(而不是根節點)下的名稱空間。

返回值

此函式返回一個包含名稱空間的陣列。

PHP 版本

此函式首次在 PHP 5 版本中引入,並在所有後續版本中有效。

示例

以下示例演示了 SimpleXMLElement::getDocNamespaces() 函式的用法。

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0' standalone='yes'?>
            <Tutorial xmlns:p='http://test.org/ns'>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            $result = $xml->getDocNamespaces();
            print_r($result);	  
         ?>      
      </body>
   </head>   
</html> 

這將產生以下結果:

JavaFX 535 Krishna 11 600

SimpleXMLElement Object ( 
   [@attributes] => Array ( [type] => test ) 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
   [Tutorial] => SimpleXMLElement Object 
   ( [Price] => 600 ) 
)

示例

以下是帶有可選引數的此函式示例:

<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->getDocNamespaces(TRUE, TRUE);
            var_dump($result);	 
         ?>      
      </body>
   </head>   
</html>

這將產生以下輸出:

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