라이브러리
[PHP] DOMDocument::createAttribute - 새 속성 생성
DOMDocument::createAttribute
DOMDocument::createAttribute 메소드는 새로운 Attribute 객체를 생성합니다. Attribute 객체는 DOMNodeList 에서 사용할 수 있습니다.
# 사용법
#hostingforum.kr
php
$doc = new DOMDocument();
$attribute = $doc->createAttribute('name');
# 예제
#hostingforum.kr
php
// 새로운 DOMDocument 객체를 생성합니다.
$doc = new DOMDocument();
// 새로운 Attribute 객체를 생성합니다.
$attribute = $doc->createAttribute('name');
// Attribute 객체에 값을 설정합니다.
$attribute->value = 'John Doe';
// Element 객체를 생성합니다.
$element = $doc->createElement('person');
// Element 객체에 Attribute 객체를 추가합니다.
$element->appendChild($attribute);
// Element 객체를 문서에 추가합니다.
$doc->appendChild($element);
// 문서를 출력합니다.
echo $doc->saveXML();
# 결과
#hostingforum.kr
xml
<?xml version="1.0"?>
# 설명
위 예제에서, 새로운 Attribute 객체를 생성하고, Element 객체에 추가합니다. 그리고 문서를 출력합니다. 결과는 XML 문서로, Element 객체에 Attribute 객체가 추가된 것을 볼 수 있습니다.
# 추가 예제
#hostingforum.kr
php
// 새로운 DOMDocument 객체를 생성합니다.
$doc = new DOMDocument();
// 새로운 Attribute 객체를 생성합니다.
$attribute = $doc->createAttribute('age');
// Attribute 객체에 값을 설정합니다.
$attribute->value = '30';
// Element 객체를 생성합니다.
$element = $doc->createElement('person');
// Element 객체에 Attribute 객체를 추가합니다.
$element->appendChild($attribute);
// Element 객체를 문서에 추가합니다.
$doc->appendChild($element);
// 새로운 Attribute 객체를 생성합니다.
$attribute = $doc->createAttribute('city');
// Attribute 객체에 값을 설정합니다.
$attribute->value = 'Seoul';
// Element 객체에 Attribute 객체를 추가합니다.
$element->appendChild($attribute);
// 문서를 출력합니다.
echo $doc->saveXML();
# 결과
#hostingforum.kr
xml
<?xml version="1.0"?>
# 설명
위 예제에서, 새로운 Attribute 객체를 생성하고, Element 객체에 추가합니다. 그리고 Element 객체에 추가한 Attribute 객체를 다시 Element 객체에 추가합니다. 결과는 XML 문서로, Element 객체에 두 개의 Attribute 객체가 추가된 것을 볼 수 있습니다.
# 참고
* [DOMDocument::createAttribute](https://www.php.net/manual/en/domdocument.createattribute.php)
* [DOMElement::appendChild](https://www.php.net/manual/en/domelement.appendChild.php)
* [DOMDocument::saveXML](https://www.php.net/manual/en/domdocument.savexml.php)
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.