DOMDocument->getElementById()

(no version information, might be only in CVS)

DOMDocument->getElementById() -- Searches for an element with a certain id

Описание

class DOMDocument {

DOMElement getElementById ( string elementId )

}

This function is similar to DOMDocument->getElementsByTagName() but searches for an element with a given id.

According to the DOM standard this requires a DTD which defines the attribute ID to be of type ID. You need to validate your document with DOMDocument->validate() or DOMDocument->validateOnParse before using this function.

Список параметров

elementId

The unique id value for an element.

Возвращаемые значения

Returns the DOMElement or NULL if the element is not found.

Примеры

Пример 1. DOMDocument->getElementById() Example

<?php

$doc
= new DomDocument;

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

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

?>

Результат выполнения данного примера:

The element whose id is books is: chapter

Смотрите также

DOMDocument->getElementsByTagName()



DOMDocument->getElementById()
richard at 2006 dot atterer dot net
05-Jun-2006 12:51
OK, so maybe PHP's getElementById() is behaving according to spec, but it does not have the behaviour that people *want*...
Below is a global getElementById() function (i.e. not a DOMDocument method) which recursively searches through a document for a certain id value.

/* Find the first element in the document (sub)tree whose id attribute
   has the value $id. By default, the entire document is searched - pass
   a non-NULL value in $node to search only the subtree below $node. */
function getElementById(DOMDocument $doc, /*string*/ $id,
                       DOMNode $node = NULL) {
  if ($node === NULL) return getElementById($doc, $id, $doc->documentElement);
  $children = $node->childNodes;
  for ($i = 0; $i < $children->length; ++$i) {
   $elem = $children->item($i);
   if (!($elem instanceof DOMElement)) continue;
   if ($elem->getAttribute('id') == $id) return $elem;
   $ret = getElementById($doc, $id, $elem);
   if ($ret !== NULL) return $ret;
  }
  return NULL;
}
Tangui dot Le-Pense at laposte dot net
08-Apr-2006 06:00
Validating a document from a DTD so as to use getElementById is sometimes impossible (for example when the head and body elements are not included yet in a XHtml document : the validation failed).
Fortunately, xml:id is supported by this function :)
That may be useful.
http://www.w3.org/TR/xml-id/
20-Dec-2005 07:04
If you're trying to use getElementById with a xml file validated on a xsd file you must first use the schemaValidate function or getElementById will return null
Example:

  $dom = new DomDocument();
  $dom->load("users.xml");
  $dom->schemaValidate("users.xsd");

  $curruser = $dom->getElementById($user->name);
bart at mediawave dot nl
11-Sep-2005 04:46
It seems getElementById works fine without setting validateOnParse to true. Which is nice since setting this to true caused some performance problems with my script.
rafaeltovar at nodo50 dot org
04-Aug-2005 01:31
Import a part to xml file to node of DOMDocument:

You have do put the correct DTD for ID attribute on XML file:

//----menu.xml----

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE menu [
   <!ELEMENT menu (item+)>
   <!ATTLIST menu id ID #REQUIRED>
   <!ELEMENT item (name, url)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT url (#PCDATA)>
]>

<menu id="menu">
   <item>
       <name>item 1</name>
       <url>item1.php</url>
   </item>
   <item>
       <name>item 2</name>
       <url>item2.php</url>
   </item>
   <item>
       <name>item 3</name>
       <url>item3.php</url>
   </item>
   <item>
       <name>item 4</name>
       <url>item4.php</url>
   </item>
</menu>
//--------------------

<?
 $dom 
= new DOMDocument('1.0', 'UTF-8');
 
$page = $dom->createElement('page');
 
$page = $dom->appendChild($page);
 
 
// Load a file
 
$domMenu  = new DOMDocument();
 
$domMenu->validateOnParse = true;
 
$domMenu->load("xml/menu.xml");
 
 
// Use getElementById()
 
$element=$domMenu->getElementById("menu");
 
 
$menu = $dom->importNode($element, true);
 
 
$page->appendChild($menu);

?>
chregu at php dot net
02-Jul-2004 04:37
http://wiki.bitflux.org/GetElementById_Pitfalls has an overview, of how to make getElementById work with aribtrary XML documents.

<DOMDocument->createTextNode()DOMDocument->getElementsByTagName()>
 Last updated: Tue, 15 Nov 2005