PHP SimpleXMLElement::children() 函式



定義和用法

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

SimpleXMLElement::attributes() 函式查詢 SimpleXMLElement 物件中的屬性及其值,並將其返回。

語法

SimpleXMLElement::attributes([$namespace, $is_prefix]);

引數

序號 引數和說明
1

namespace(可選)

這是一個字串值,表示屬性所屬的名稱空間。

2

Is_prefix(可選)

這是一個布林值,表示指定的名稱空間是字首 (TRUE) 還是 URL (FALSE)。

返回值

這將返回一個 SimpleXMLElement 類的物件,表示子節點。

PHP 版本

此函式最初在 PHP 5 版本中引入,並在所有後續版本中均有效。

示例

以下示例演示了 SimpleXMLIterator::children() 函式的用法。

<html>
   <head>
      <body>
         <?php
            $str="<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name type = 'programming'>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            print("Child nodes: <br>");
            foreach ($xml->children() as $child){
               print($child . "<br>");
            }
         ?>      
      </body>
   </head>   
</html>

這將產生以下結果:

Child nodes:
JavaFX
535
Krishna
11

示例

以下是此函式的另一個示例,我們嘗試獲取 XML 檔案中的子節點:

<html>
   <head>      
      <body>         
         <?php
            $doc = new DOMDocument;
            $xml = simplexml_load_file("data.xml");
            //file to SimpleXMLElement 
            $xml = simplexml_import_dom($xml);

            print("Child nodes: <br>");
            foreach ($xml->children() as $child){
                print($child . "<br>");
            }			
         ?>
      </body>
   </head>
</html>

這將產生以下結果:

Child nodes:
CoffeeScript
235
Kasyap
2.5.1 
php_function_reference.htm
廣告