DomDocument->get_element_by_id

(no version information, might be only in CVS)

DomDocument->get_element_by_id --  Searches for an element with a certain id

说明

domelement DomDocument->get_element_by_id ( string id )

This function is similar to domdocument_get_elements_by_tagname() 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, though the current implementation simply does an xpath search for "//*[@ID = '%s']". This does not comply to the DOM standard which requires to return null if it is not known which attribute is of type id. This behaviour is likely to be fixed, so do not rely on the current behaviour.

See also domdocument_get_elements_by_tagname()


add a note add a note User Contributed Notes
noel at rescommunes dot ca
16-Feb-2006 11:40
get_element_by_id does not work on my version of php :(
This reallyGet_element_by_id function is a easy replacenemt.

<?php
 
/**
   * Get a node by its id 'attribute' value
   *
   * The attribute name id has to be spelled lowercase in xml file
   *
   * @return node on success and null in failure.
   * @param string An id to look for.
   */

 
function reallyGet_element_by_id( $str_id  ){
  
$node_with_id  = null;
  
$xpath = xpath_new_context($this->dom);
  
$xpresult = xpath_eval($xpath, "//@id");
   foreach(
$xpresult->nodeset as $node ){
     if (
$node->value == $str_id)
       return
$node->parent_node( );
   }
   return
$node_with_id;
  }

?>
etienne dot anken at eivd dot ch
03-Dec-2002 11:51
This class is useful for people who haven't the latest release of PHP and would like to search an element with a known ID through a collection of nodes.

Example to use it :

$domDoc = xmldoc("<root><entity id='e1'></entity><entity id='e2'><titi id='e5'></titi></entity></root>");
  
$root = $domDoc->root();
$children = $root->child_nodes();
  
$researchObject = new searchElementById("e5", $children);
$resultElement = $researchObject->beginSearching();

The source of the class :

class searchElementById {
      
  //Class which can find and return an XML node with a specified ID
  var $numberElements=0;
  var $numberAttributes=0;
  var $idToFind;
  var $tabElements;
      
  //Initialization function which accept 2 parameters :
  //$paramIdToFind : value of the ID to find
  //$paramTabElements : array of nodes which contains the element to find
  function searchElementById($paramIdToFind, $paramTabElements) {
   //Initialization of class variables
   $this->idToFind = $paramIdToFind;
   $this->tabElements = $paramTabElements;
  }
  
  //Function starting the research
  function beginSearching() {
   //Search and return the element found
   return $this->searchID($this->tabElements);
  }
      
  //Recursive function searching the desired node
  function searchID($tabElements) {
  
   //Variable of the number of elements
   $i = 0;
   //Variable of the number of attributes
   $j = 0;
  
   //Number of nodes in the elements' array
   $nbreNoeuds = count($tabElements);
                  
   //Loop on all elements
   for ($i=0;$i<$nbreNoeuds;$i++) {
              
     //Incrementation of the class variable couting the total number of elements
     $this->compteurElements++;
              
     //Extraction of the attributes of the current element
     $tabAttributs = $tabElements[$i]->attributes();
     //Number of attributes in the current element
     $nbreAttributs = count($tabAttributs);
     //Loop on all attributes
     for ($j=0;$j<$nbreAttributs;$j++) {
                  
       //Incrementation of the class variable couting the total number of attributes
       $this->numberAttributes++;

       //Test if the current attribute is the attribute to find
       if ($tabAttributs[$j]->value==$this->idToFind) {
         //If yes, return the current element
         return $tabElements[$i];
       }
                  
     }
              
     //Search children nodes of the current element
     $children = $tabElements[$i]->child_nodes();
     if (!is_bool($children)) {
       //If the current element has children, call recursively this function
       $result = $this->searchID($children);
       //If the result is not boolean, return the array
       if (!is_bool($result)) {
         return $result;
       }               
     }
   }
          
   return false;
  
  }
      
}