Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMDocument->registerNodeClass()

DOMDocument->registerNodeClass()

Register extended class used to create base node type ()

Example 526. 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>

Related Examples ( Source code ) » dom domdocument registernodeclass



Code Examples / Notes » dom domdocument registernodeclass

nobody

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."
";
echo $doc->childNodes->item(0)->childNodes->item(1)->prop."
";
?>
Result:
test2
Should be: test1
test2
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

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));
}


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker