PHP Conference Japan 2024

SimpleXMLElement::children

(PHP 5, PHP 7, PHP 8)

SimpleXMLElement::children尋找指定節點的子節點

說明

公開 SimpleXMLElement::children(?字串 $namespaceOrPrefix = null, 布林值 $isPrefix = false): ?SimpleXMLElement

此方法會尋找元素的子元素。結果遵循一般的迭代規則。

注意SimpleXML 有一個規則,會將迭代屬性添加到大多數方法中。它們無法使用 var_dump() 或任何其他可以檢查物件的工具來檢視。

參數

namespaceOrPrefix

一個 XML 命名空間。

isPrefix

如果 isPrefixtrue,則 namespaceOrPrefix 將被視為前綴。如果為 false,則 namespaceOrPrefix 將被視為命名空間 URL

回傳值

會傳回一個 SimpleXMLElement 元素,無論該節點是否有子節點,除非該節點代表一個屬性,在這種情況下會傳回 null

範例

範例 #1 遍歷 children() 類似陣列

<?php
$xml
= new SimpleXMLElement(
'<person>
<child role="son">
<child role="daughter"/>
</child>
<child role="daughter">
<child role="son">
<child role="son"/>
</child>
</child>
</person>'
);

foreach (
$xml->children() as $second_gen) {
echo
' 這人產下一 ' . $second_gen['role'];

foreach (
$second_gen->children() as $third_gen) {
echo
' 這人生了個 ' . $third_gen['role'] . ';';

foreach (
$third_gen->children() as $fourth_gen) {
echo
' 而那 ' . $third_gen['role'] .
' 生了個 ' . $fourth_gen['role'];
}
}
}
?>

以上範例將輸出

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son

範例 #2 使用命名空間

<?php
$xml
= '<example xmlns:foo="my.foo.urn">
<foo:a>Apple</foo:a>
<foo:b>Banana</foo:b>
<c>Cherry</c>
</example>'
;

$sxe = new SimpleXMLElement($xml);

$kids = $sxe->children('foo');
var_dump(count($kids));

$kids = $sxe->children('foo', TRUE);
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn');
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn', TRUE);
var_dump(count($kids));

$kids = $sxe->children();
var_dump(count($kids));
?>
int(0)
int(2)
int(2)
int(0)
int(1)

另請參閱

新增備註

使用者貢獻的備註 4 則備註

aero
17 年前
這是一個簡單的遞迴函式,可將 XML 資料轉換為類似 E4X 的語法,例如 root.child.value = foobar

<?php
error_reporting
(E_ALL);

$xml = new SimpleXMLElement(
'<Patriarch>
<name>Bill</name>
<wife>
<name>Vi</name>
</wife>
<son>
<name>Bill</name>
</son>
<daughter>
<name>Jeri</name>
<husband>
<name>Mark</name>
</husband>
<son>
<name>Greg</name>
</son>
<son>
<name>Tim</name>
</son>
<son>
<name>Mark</name>
</son>
<son>
<name>Josh</name>
<wife>
<name>Kristine</name>
</wife>
<son>
<name>Blake</name>
</son>
<daughter>
<name>Liah</name>
</daughter>
</son>
</daughter>
</Patriarch>'
);

RecurseXML($xml);

function
RecurseXML($xml,$parent="")
{
$child_count = 0;
foreach(
$xml as $key=>$value)
{
$child_count++;
if(
RecurseXML($value,$parent.".".$key) == 0) // 沒有子節點,也就是「葉節點」
{
print(
$parent . "." . (string)$key . " = " . (string)$value . "<BR>\n");
}
}
return
$child_count;
}

?>

輸出結果....

.name = Bill
.wife.name = Vi
.son.name = Bill
.daughter.name = Jeri
.daughter.husband.name = Mark
.daughter.son.name = Greg
.daughter.son.name = Tim
.daughter.son.name = Mark
.daughter.son.name = Josh
.daughter.son.wife.name = Kristine
.daughter.son.son.name = Blake
.daughter.son.daughter.name = Liah
Sebastian
19 年前
快速補充說明

如果您需要存取包含連字號的子節點,您需要用 {""} 將其括起來。

例如
<?php
foreach ($domain->domain-listing as $product) {
}
?>

由於連字號的緣故,上面的例子無法正常運作。您需要使用以下方式:
<?php
foreach ($domain->{"domain-listing"} as $product) {
}
?>

至少對我來說,第二個例子可以完美運作。
transglobe at gmx dot de
16 年前
我對 RecurseXML 函式採用了稍微不同的方法。肚子餓的時候我在程式碼上遇到問題,因為它只覆寫了兩個 <maincourse>。所以我做了以下的修改:

<?php

$xml
= new SimpleXMLElement(
'<meal>
<type>午餐</type>
<time>12:30</time>
<menu>
<entree>沙拉</entree>
<maincourse>
<part>船</part>
<part>牛排</part>
</maincourse>
<maincourse>
<part>魚</part>
<part>米飯</part>
</maincourse>
<maincourse>
<part>紅酒</part>
<part>起司</part>
</maincourse>
</menu>
</meal>'
);

$vals = array();
RecurseXML($xml,$vals);

foreach(
$vals as $key=>$value)
print(
"{$key} = {$value}<BR>\n");

function
RecurseXML($xml,&$vals,$parent="") {

$childs=0;
$child_count=-1; # 並非必要。
$arr=array();
foreach (
$xml->children() as $key=>$value) {
if (
in_array($key,$arr)) {
$child_count++;
} else {
$child_count=0;
}
$arr[]=$key;
$k=($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count";
$childs=RecurseXML($value,$vals,$k);
if (
$childs==0) {
$vals[$k]= (string)$value;
}
}

return
$childs;
}

?>
輸出如下:
type.0 = 午餐
time.0 = 12:30
menu.0.entree.0 = 沙拉
menu.0.maincourse.0.part.0 = 船
menu.0.maincourse.0.part.1 = 牛排
menu.0.maincourse.0 =
menu.0.maincourse.1.part.0 = 魚
menu.0.maincourse.1.part.1 = 米飯
menu.0.maincourse.1 =
menu.0.maincourse.2.part.0 = 紅酒
menu.0.maincourse.2.part.1 = 起司
menu.0.maincourse.2 =
menu.0 =

(不是很漂亮,但它解決了我的問題...)
boan dot web at outlook dot com
5 年前
在這種情況下,SimpleXMLElement::children 可能會返回 null

<?php
$xml
= '
<root attr="Hello"/>
'
;

$sxe = new SimpleXMLElement($xml);

$sxe_xpath = $sxe->xpath('/root/@attr')[0];

$children = $sxe_xpath->children();

var_export($children); // 結果為 null
?>
To Top