
SimpleXMLElement::key를 사용하여 XML 데이터의 키를 가져올 때, 키가 여러 개 있을 때는 foreach문을 사용하여 하나씩 가져올 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement($xml_string);
foreach ($xml->xpath('//*') as $key => $value) {
echo $key . ': ' . $value . "n";
}
키가 없을 때는 if문을 사용하여 키가 존재하는지 확인할 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement($xml_string);
if (isset($xml->key)) {
echo $xml->key . "n";
} else {
echo "키가 없습니다.n";
}
또는, try-catch문을 사용하여 예외를 처리할 수 있습니다.
#hostingforum.kr
php
$xml = new SimpleXMLElement($xml_string);
try {
echo $xml->key . "n";
} catch (Exception $e) {
echo "키가 없습니다.n";
}
이러한 방법 중 하나를 사용하여 키가 없을 때의 처리를 할 수 있습니다.
2025-07-12 11:36