
SimpleXMLElement::children 메소드는 XML 요소의 자식 요소를 반환합니다. 이 메소드는 파라미터를 받지 않습니다. 따라서, 특정 요소의 자식 요소를 가져올 때는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
$children = $xml->children();
foreach ($children as $child) {
echo $child->getName() . "n";
}
이 예제에서는 root 요소의 모든 자식 요소를 가져와서 출력합니다. 만약 특정 요소의 자식 요소를 가져올 때는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
$child = $xml->child1;
$children = $child->children();
foreach ($children as $child) {
echo $child->getName() . "n";
}
이 예제에서는 root 요소의 child1 요소의 자식 요소를 가져와서 출력합니다.
만약 특정 요소의 자식 요소를 가져올 때는, 요소의 이름을 사용하여 자식 요소를 찾을 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
$child = $xml->addChild('child1');
$child->addChild('grandchild1');
$child->addChild('grandchild2');
$grandchildren = $child->children();
foreach ($grandchildren as $grandchild) {
echo $grandchild->getName() . "n";
}
이 예제에서는 root 요소의 child1 요소에 grandchild1, grandchild2 요소를 추가하고, child1 요소의 자식 요소를 가져와서 출력합니다.
이러한 예제를 통해 SimpleXMLElement::children 메소드를 사용하여 자식 요소를 가져올 수 있는 방법을 이해할 수 있습니다.
2025-05-17 18:04