SimpleXML SimpleXMLElement::children
(PHP 5, PHP 7, PHP 8)
SimpleXMLElement::children — 주어진 노드의 자식 찾기
설명
public SimpleXMLElement::children(?string $namespaceOrPrefix
= null
, bool $isPrefix
= false
): ?SimpleXMLElement
이 메서드는 요소의 자식을 찾습니다. 결과는 일반적인 반복 규칙을 따릅니다.
참고: SimpleXML은 대부분의 메서드에 반복 속성을 추가하는 규칙을 만들었습니다. var_dump() 또는 객체를 검사할 수 있는 다른 것을 사용하여 볼 수 없습니다.
매개변수
namespaceOrPrefix
- XML 네임스페이스.
isPrefix
isPrefix
가true
이면namespaceOrPrefix
는 접두사로 간주됩니다.false
인 경우namespaceOrPrefix
는 네임스페이스 URL로 간주됩니다.
반환 값
노드가 속성을 나타내는 경우가 아니면 노드에 자식이 있는지 여부에 관계없이 SimpleXMLElement 요소를 반환합니다. 이 경우 null
이 반환됩니다.
Examples
예제 #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 ' 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'];
}
}
}
?>
위의 예는 다음을 출력합니다.
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)
기타
- SimpleXMLElement::count() - 요소의 자식을 계산합니다.
- count() - 배열 또는 Countable 개체의 모든 요소를 셉니다.