
SimpleXMLElement::children() 메서드는 현재 요소의 자식 요소를 반환합니다. 따라서, 'person' 요소의 하위 요소를 가져올 때는 'root' 요소의 하위 요소인 'person' 요소를 참조해야 합니다.
예를 들어, 다음과 같이 'root' 요소의 하위 요소인 'person' 요소를 참조하여 하위 요소를 가져올 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('John30');
$root = $xml->root; // 'root' 요소 참조
$person = $root->person; // 'root' 요소의 하위 요소인 'person' 요소 참조
$children = $person->children(); // 'person' 요소의 하위 요소 참조
print_r($children);
이 코드는 'name' 요소와 'age' 요소를 가져올 수 있습니다.
또한, 'root' 요소의 하위 요소인 'person' 요소의 하위 요소를 가져올 때는 foreach 문을 사용하여 하위 요소를 반복적으로 참조할 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('John30Jane25');
$root = $xml->root; // 'root' 요소 참조
$children = $root->person; // 'root' 요소의 하위 요소인 'person' 요소 참조
foreach ($children as $person) {
echo $person->name . "n"; // 'name' 요소 참조
echo $person->age . "n"; // 'age' 요소 참조
}
이 코드는 'root' 요소의 하위 요소인 'person' 요소의 하위 요소를 반복적으로 참조하여 'name' 요소와 'age' 요소를 가져올 수 있습니다.
2025-05-12 03:22