PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument::getElementsByTagName> <DOMDocument::createTextNode
Last updated: Fri, 10 Oct 2008

view this page in

DOMDocument::getElementById

(No version information available, might be only in CVS)

DOMDocument::getElementByIdCherche un élément avec un certain identifiant

Description

DOMElement DOMDocument::getElementById ( string $elementId )

Cette fonction est similaire à la fonction DOMDocument::getElementsByTagName mais cherche un élément avec un identifiant donné.

Pour que cette fonction fonctionne, vous devez soit définir les attributs ID avec DOMElement::setIdAttribute ou définir une DTD qui définit un attribut devant être de type ID. Dans le dernier cas, vous devez valider votre document avec DOMDocument::validate ou DOMDocument::validateOnParse avant d'utiliser cette fonction.

Liste de paramètres

elementId

La valeur de l'identifiant unique pour un élément.

Valeurs de retour

Retourne un DOMElement ou NULL si l'élément n'est pas trouvé.

Exemples

Exemple #1 Exemple avec DOMDocument->getElementById()

<?php

$doc 
= new DomDocument;

// Nous devons valider notre document avant de nous référer à l'ID
$doc->validateOnParse true;
$doc->Load('book.xml');

echo 
"L'élément dont l'ID est 'book' est : " $doc->getElementById('books')->tagName "\n";

?>

L'exemple ci-dessus va afficher :

L'élément dont l'ID est 'book' est : chapter


add a note add a note User Contributed Notes
DOMDocument::getElementById
guillaume dot crico at gmail dot com
01-Oct-2008 04:55
You don't want to use "xml:id" ?
Here is the relaxNG trick (with a generic schema):
(tested with libxml 2.6.26)

<?php
$doc
= new DOMDocument();
$doc->load(...);

$rng = '
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element>
            <anyName/>
            <ref name="anythingID"/>
        </element>
    </start>
    <define name="anythingID">
        <zeroOrMore>
            <choice>
                <element>
                    <anyName/>
                    <ref name="anythingID"/>
                </element>
                <attribute name="id">
                    <data type="ID"/>
                </attribute>
                <zeroOrMore>
                    <attribute><anyName/></attribute>
                </zeroOrMore>
                <text/>
            </choice>
        </zeroOrMore>
    </define>
</grammar>
'
;

$doc->relaxNGValidateSource($rng);
var_dump($doc->getElementById('id1'));
?>


Note that ID values must be valid ones :
  - integers do no work!
  - @see http://www.w3.org/TR/REC-xml/#id
  - => (Letter | '_' | ':') ( Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender  )*
dagfinn at reiersol dot com
18-Sep-2007 11:06
Here is an updated link to the getElementById() pitfalls page:

http://wiki.flux-cms.org/display/BLOG/GetElementById+Pitfalls
simon at somewhere dot com
30-Jan-2007 05:07
SAVE YOURSELF A MAJOR HEADACHE AND A LOT OF SEARCHING THROUGH DOCUMENTATION -

Instead of using $object->setAttribute('id', 'id_name_here')
USE THIS: $object->setAttribute('xml:id', 'id_name_here')

Then, to get the node value: $domDocumentObject->getElementById('id_name_here');

The xml:id attribute should AUTOMATICALLY be defined!!

Woohoo!  That was easy......
jonbarnett at gmail dot com
01-Nov-2006 05:36
If your XML document does not have a DTD that defines the "id" attribute as an ID, then the easiest thing to do is to use XPath->query() to find an element that matches "//[@id='x']"
Tangui dot Le-Pense at laposte dot net
08-Apr-2006 03: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 04: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 01: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.
chregu at php dot net
02-Jul-2004 01:37
http://wiki.bitflux.org/GetElementById_Pitfalls has an overview, of how to make getElementById work with aribtrary XML documents.

 
show source | credits | sitemap | contact | advertising | mirror sites