DOM DOMDocument::getElementById

(PHP 5, PHP 7, PHP 8)

DOMDocument::getElementById — 특정 ID를 가진 요소를 검색합니다.


설명

public DOMDocument::getElementById(string $elementId): ?DOMElement

이 함수는 DOMDocument::getElementsByTagName과 유사하지만 주어진 ID를 가진 요소를 검색합니다.

이 함수는 작동하려면 DOMElement::setIdAttribute 또는 속성을 ID 유형으로 정의하는 DTD를 사용하여 일부 ID 속성을 설정해야 합니다. 나중의 경우 이 함수를 사용하기 전에 DOMDocument::validate 또는 DOMDocument::$validateOnParse로 문서의 유효성을 검사해야 합니다.


매개변수

elementId
요소의 고유 ID 값입니다.

반환 값

DOMElement를 반환하거나 요소가 없으면 null을 반환합니다.


Examples

예제 #1 DOMDocument::getElementById() 예제

다음 예제에서는 다음을 포함하는 book.xml을 사용합니다.

                  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
  <!ELEMENT books   (book+)>
  <!ELEMENT book    (title, author+, xhtml:blurb?)>
  <!ELEMENT title   (#PCDATA)>
  <!ELEMENT blurb   (#PCDATA)>
  <!ELEMENT author  (#PCDATA)>
  <!ATTLIST books   xmlns        CDATA  #IMPLIED>
  <!ATTLIST books   xmlns:xhtml  CDATA  #IMPLIED>
  <!ATTLIST book    id           ID     #IMPLIED>
  <!ATTLIST author  email        CDATA  #IMPLIED>
]>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<books xmlns="http://books.php/" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <book id="php-basics">
    <title>PHP Basics</title>
    <author email="jim.smith@basics.php">Jim Smith</author>
    <author email="jane.smith@basics.php">Jane Smith</author>
    <xhtml:blurb><![CDATA[
<p><em>PHP Basics</em> provides an introduction to PHP.</p>
]]></xhtml:blurb>
  </book>
  <book id="php-advanced">
    <title>PHP Advanced Programming</title>
    <author email="jon.doe@advanced.php">Jon Doe</author>
  </book>
</books>
                  
                
                  
<?php

$doc = new DomDocument;

// We need to validate our document before referring to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');

echo "The element whose id is 'php-basics' is: " . $doc->getElementById('php-basics')->tagName . "\n";

?>
                  
                

위의 예는 다음을 출력합니다.

The element whose id is 'php-basics' is: book
                

기타