
DOMXPath::__construct 메서드는 XML 문서를 파싱하기 위해 사용되는 메서드입니다. 이 메서드는 XML 문서를 파싱한 후 DOMDocument 객체에 접근할 수 있도록 도와줍니다.
DOMXPath::__construct 메서드는 다음 형식으로 사용됩니다.
#hostingforum.kr
php
$xpath = new DOMXPath($document);
위의 코드는 DOMDocument 객체를 이용해 DOMXPath 객체를 생성합니다.
DOMXPath::__construct 메서드는 XML 문서를 파싱한 후 XPath 표현식을 사용하여 XML 문서 내의 요소를 검색할 수 있도록 도와줍니다.
예를 들어, 다음과 같이 XML 문서를 파싱한 후 DOMXPath 객체를 사용하여 XML 문서 내의 요소를 검색할 수 있습니다.
#hostingforum.kr
php
$xml = 'John30';
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXPath($document);
$persons = $xpath->query('//person');
foreach ($persons as $person) {
echo $person->getElementsByTagName('name')->item(0)->nodeValue . "n";
echo $person->getElementsByTagName('age')->item(0)->nodeValue . "n";
}
이 예제에서는 XML 문서 내의 `person` 요소를 검색하고, 각 `person` 요소 내의 `name`과 `age` 요소의 값을 출력합니다.
이러한 예제를 통해 DOMXPath::__construct 메서드의 사용법을 이해할 수 있습니다.
2025-06-05 05:39