PHP - SimpleXMLElement::getNameSpaces() 函式



定義和用法

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

SimpleXMLElement::getNamespaces() 函式檢索並返回文件中使用的名稱空間。

語法

SimpleXMLElement::getNamespaces([$recursive]);

引數

序號 引數及描述
1

recursive (可選)

這是一個布林值,如果傳遞 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_function_reference.htm
廣告