
SimpleXMLElement::attributes 메서드는 XML 태그의 속성을 배열로 반환하는 메서드입니다.
예를 들어, 다음 XML 코드에서 "name" 속성을 가져올 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('30');
$name = (string)$xml->person['name'];
echo $name; // John
만약 "name" 속성이 여러 태그에 있는 경우, 각 태그의 속성을 별도로 가져와야 합니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('3025');
$persons = $xml->xpath('//person');
foreach ($persons as $person) {
echo (string)$person['name'] . "n";
}
// John
// Jane
또한, SimpleXMLElement::attributes 메서드는 모든 속성을 반환하므로, 필요하지 않은 속성을 제거하는 것이 좋습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement('30');
$attributes = $xml->person->attributes();
unset($attributes['age']);
echo (string)$xml->person['name']; // John
2025-05-11 07:07