PHP children() 函數
實例
查找 note 節點的子節點:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
foreach ($xml->children() as $child)
{
echo "Child node: " . $child . "<br>";
}
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
foreach ($xml->children() as $child)
{
echo "Child node: " . $child . "<br>";
}
?>
定義和用法
children() 函數查找指定節點的子節點。
語法
children(ns,is_prefix);
參數 | 描述 |
---|---|
ns | 可選。規定一個 XML 命名空間。 |
is_prefix | 可選。規定一個布爾值。如果值為 TRUE,則 ns 是首碼。如果值為 FALSE,則 ns 是命名空間 URL。 |
技術細節
返回值: | 返回一個 SimpleXMLElement 對象。 |
---|---|
PHP 版本: | 5.0.1+ |
PHP 更新日誌: | 新增了 is_prefix 參數。 |
更多實例
實例 1
查找 body 節點的子節點:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body><span>Important!</span> Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
foreach ($xml->body[0]->children() as $child)
{
echo "Child node: " . $child . "<br>";
}
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body><span>Important!</span> Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
foreach ($xml->body[0]->children() as $child)
{
echo "Child node: " . $child . "<br>";
}
?>
