라이브러리
[PHP] SimpleXMLElement::attributes - 요소의 속성을 식별합니다.
SimpleXMLElement::attributes는 PHP의 SimpleXMLElement 클래스에서 사용할 수 있는 메소드입니다. 이 메소드는 XML 요소의 속성을 접근하거나 설정할 수 있는 방법을 제공합니다.
SimpleXMLElement::attributes 사용법
SimpleXMLElement::attributes 메소드는 XML 요소의 속성을 배열로 반환합니다. 속성 이름은 키로, 속성 값은 값으로 사용됩니다.
# 예제 1: 속성 접근
#hostingforum.kr
php
$xml = new SimpleXMLElement('John');
$attributes = $xml->name->attributes();
echo $attributes['name']; // John
# 예제 2: 속성 설정
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
$xml->name->attributes()['name'] = 'Jane';
echo $xml->asXML(); //
# 예제 3: 속성 추가
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
$xml->name->attributes()['age'] = 30;
echo $xml->asXML(); //
# 예제 4: 속성 삭제
#hostingforum.kr
php
$xml = new SimpleXMLElement('');
unset($xml->name->attributes()['age']);
echo $xml->asXML(); //
속성 이름에 공백이 있는 경우
속성 이름에 공백이 있는 경우, 속성 이름을 따옴표로 감싸야 합니다.
# 예제 5: 속성 이름에 공백이 있는 경우
#hostingforum.kr
php
$xml = new SimpleXMLElement('John Doe');
$attributes = $xml->name->attributes();
echo $attributes['name']; // John Doe
$xml->name->attributes()['name'] = 'Jane Doe';
echo $xml->asXML(); //
결론
SimpleXMLElement::attributes 메소드는 XML 요소의 속성을 쉽게 접근하고 설정할 수 있는 방법을 제공합니다. 속성 이름에 공백이 있는 경우, 속성 이름을 따옴표로 감싸야 합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.