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, 05 Sep 2008

view this page in

DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0)

DOMDocument::registerNodeClassRegister extended class used to create base node type

Description

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

This method allows you to register your own extended DOM class to be used afterward by the PHP DOM extension.

This method is not part of the DOM standard.

Parameters

baseclass

The DOM class that you want to extend. You can find a list of these classes in the chapter introduction.

Of course, you won't be able to register a class extending DOMDocument but you can always start your document by instanciating your own extending class.

extendedclass

Your extended class name. If NULL is provided, any previously registered class extending baseclass will be removed.

Return Values

Returns TRUE on success or FALSE on failure.

ChangeLog

Version Description
PHP 5.2.2 Prior to 5.2.2, a previously registered extendedclass had to be unregistered before being able to register a new class extending the same baseclass .

Examples

Example #1 Adding a new method to DOMElement to ease our code

<?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');

// From now on, adding an element to another costs only one method call ! 
$root $doc->setRoot('root');
$child $root->appendElement('child');
$child->setAttribute('foo''bar');

echo 
$doc->saveXML();

?>

The above example will output:

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



add a note add a note User Contributed Notes
DOMDocument::registerNodeClass
Anonymous
23-Jul-2008 09: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 11: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 08: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