當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。