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

search for in the

DOMDocument::relaxNGValidate> <DOMDocument::normalizeDocument
Last updated: Fri, 29 Aug 2008

view this page in

DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0)

DOMDocument::registerNodeClassEnregistre la classe étendue utilisée pour créer un type de base de noeud

Description

bool DOMDocument::registerNodeClass ( string $baseclass , string $extendedclass )

Cette méthode vous permet d'enregistrer votre propre classe étendue DOM à utiliser ensuite dans l'extension DOM de PHP.

Cette méthode ne fait pas partie du standard DOM.

Liste de paramètres

baseclass

La classe DOM qui vous voulez étendre. Vous pouvez trouver une liste de ces classes dans l'introduction du chapitre.

Bien sûr, vous ne pouvez pas enregistrer une classe étendant DOMDocument mais vous pouvez toujours commencer votre document en instanciant votre propre classe étendue.

extendedclass

Le nom de votre classe étendue. Si la valeur NULL est fournie, toutes les classes enregistrées précédemment étendant baseclass seront supprimées.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Historique

Version Description
PHP 5.2.2 Avant la version 5.2.2, une classe extendedclass précédemment enregistrée devait être supprimées avant de pouvoir en enregistrer une nouvelle étendant la même classe baseclass .

Exemples

Exemple #1 Ajout d'un nouvelle méthode à DOMElement

<?php

class myElement extends DOMElement {
   function 
appendElement($name) { 
      return 
$this->appendChild(new myElement($name));
   }
}

class 
myDocument extends DOMDocument {
   function 
setRoot($name) { 
      return 
$this->appendChild(new myElement($name));
   }
}

$doc = new myDocument();
$doc->registerNodeClass('DOMElement''myElement');

// À partir d'ici, l'ajout d'un élément à un autre se fait en un seul appel !
$root $doc->setRoot('root');
$child $root->appendElement('child');
$child->setAttribute('foo''bar');

echo 
$doc->saveXML();

?>

L'exemple ci-dessus va afficher :

<?xml version="1.0"?>
<root><child foo="bar"/></root>



add a note add a note User Contributed Notes
DOMDocument::registerNodeClass
Anonymous
23-Jul-2008 03:03
If you want to get child nodes of some node, try something like this:

<?php
// check
$doc = new DomDocument();
// load XML file
$doc->loadXML($items);
// Now loop through and get root
// get those we need (all of them actually)
$Elements = $doc->getElementsByTagName("item");
// loop through
foreach($Elements as $elem)
 {
 
// check if the element is a root one
 
if($elem->parentNode->getAttribute("link"))
 {}
 else
 {       
 
// if it IS then check if it has child nodes
 
if($elem->hasChildNodes())
  {
  
// if it has them just got only child nodes
  
$Children = $elem->getElementsByTagName("item");
  
// do whatever you want with your children
 
}
 }
}
?>
nobody at dontlikespam dot com
07-Oct-2007 05:58
There's a bug (similar reported 3 years ago as #28473 but tagged as "bogus" by lazy developer) in this function. When you eg. create own DOMElement class that have some properties and then try to set those properties on several nodes, only value set on the last node will be available.

<?php
class test extends DOMElement {
   public
$prop;
}

$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement','test');
$doc->loadXML('<root><t1></t1><t2></t2></root>');

$tn = $doc->childNodes->item(0)->childNodes->item(0);
$tn->prop = 'test1'; // Set on T1 tag

$tn = $doc->childNodes->item(0)->childNodes->item(1);
$tn->prop = 'test2'; // Set on T2 tag

echo $doc->childNodes->item(0)->childNodes->item(0)->prop."<br>";
echo
$doc->childNodes->item(0)->childNodes->item(1)->prop."<br>";

?>

Result: <br>test2<br>

Should be: test1<br>test2<br>

That and fact that one can't get unique PHP object identifcator (not XML attribute) of every node (also DOMText or DOMAttr) makes this PHP's DOMXML just a simple XML traverse function and not (as should be) powerful way to do custom (and FAST - there is no problem in rewriting DOM tree to own objects but where's in that the sense of using DOM anyway?) work with XML documents.

DOM implementation in PHP sucks and it would be better to not provide one (and make users create their own) than include that crap (and make users to think that they are able to do something in notime while it takes week).
mjong at magnafacta dot com
21-Sep-2007 02:07
If you want to implement your own DOMDocument class you have to register your new document class. Otherwise $node->ownerDocument will return an object of the type DOMDocument without your extra features.

I.e.:

public function __construct($version = "1.0", $encoding = "UTF-8")
{
   parent::__construct($version, $encoding);

   parent::registerNodeClass('DOMDocument', get_class($this));
}

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