(PHP 5, PHP 7, PHP 8)
SimpleXMLElement::children — Belirtilen düğümün çocuklarını bulur
$isimalanı_veya_önek
= null
, bool$önek_var
= false
): ?SimpleXMLElement Bu yöntem nesnenin içerdiği alt düğümleri bulur. Sonuçlara foreach
döngüleri üzerinden erişilebilir.
Bilginize: SimpleXML iç içe nesnelerden (XML düğümlerinden) oluşur. Bu nedenle nesne içeriğini görmek için var_dump() ve benzerleri kullanılamaz.
Düğüm bir öznitelik için ise null
, aksi takdirde SimpleXMLElement nesnesi döner.
Örnek 1 - children()
dizimsisi
<?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 ' The person begot a ' . $second_gen['role'];
foreach ($second_gen->children() as $third_gen) {
echo ' who begot a ' . $third_gen['role'] . ';';
foreach ($third_gen->children() as $fourth_gen) {
echo ' and that ' . $third_gen['role'] .
' begot a ' . $fourth_gen['role'];
}
}
}
?>
Yukarıdaki örneğin çıktısı:
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son
Örnek 2 - İsimalanlarının kullanımı
<?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)