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::registerNodeClass基底ノード型を作成する際に使用する拡張クラスを登録する

説明

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

このメソッドにより、独自に拡張した DOM クラスを登録することができます。 これを、後で PHP DOM 拡張モジュールで使用します。

このメソッドは、DOM の標準にはないものです。

パラメータ

baseclass

拡張したい DOM クラス。クラス名の一覧は、この章の導入部にあります。

もちろん、DOMDocument を拡張したクラスを登録することはできません。 しかし、拡張したクラスのインスタンスを作成することで、常にドキュメントを開始できます。

extendedclass

拡張したクラスの名前。NULL を渡した場合は、 それまでに baseclass を拡張して作成したすべてのクラスが削除されます。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

変更履歴

バージョン 説明
PHP 5.2.2 5.2.2 より前のバージョンでは、同一の baseclass を継承した新しいクラスを登録する際には、以前に登録されていた extendedclass の登録を解除する必要がありました。

例1 新しいメソッドを 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');

// これ以降、他の要素への要素の追加が一回のメソッドコールでできるようになります!
$root $doc->setRoot('root');
$child $root->appendElement('child');
$child->setAttribute('foo''bar');

echo 
$doc->saveXML();

?>

上の例の出力は以下となります。

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



add a note add a note User Contributed Notes
DOMDocument::registerNodeClass
Anonymous
23-Jul-2008 06: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 08: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 05: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 | stats | sitemap | contact | advertising | mirror sites