DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DOMDocument::registerNodeClass β€” Register extended class used to create base node type

Опис

public DOMDocument::registerNodeClass(string $baseClass, ?string $extendedClass): true

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.

ЗастСрСТСння

The constructor of the objects of the registered node classes is not called.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΈ

baseClass

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

extendedClass

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

ЗначСння, Ρ‰ΠΎ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°ΡŽΡ‚ΡŒΡΡ

Π—Π°Π²ΠΆΠ΄ΠΈ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” true.

Π–ΡƒΡ€Π½Π°Π» Π·ΠΌΡ–Π½

ВСрсія Опис
8.4.0 DOMDocument::registerNodeClass() now has a tentative return of true.

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ΠΈ

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #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();

?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

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

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #2 Retrieving elements as custom class

<?php
class myElement extends DOMElement {
public function
__toString() {
return
$this->nodeValue;
}
}

$doc = new DOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");
$doc->registerNodeClass("DOMElement", "myElement");

$element = $doc->getElementsByTagName("child")->item(0);
var_dump(get_class($element));

// And take advantage of the __toString method..
echo $element;
?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

string(9) "myElement"
text in child

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #3 Retrieving owner document

When instantiating a custom DOMDocument the ownerDocument property will refer to the instantiated class. However, if all references to that class are removed, it will be destroyed and new DOMDocument will be created instead. For that reason you might use DOMDocument::registerNodeClass() with DOMDocument

<?php
class MyDOMDocument extends DOMDocument {
}

class
MyOtherDOMDocument extends DOMDocument {
}

// Create MyDOMDocument with some XML
$doc = new MyDOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");

$child = $doc->getElementsByTagName("child")->item(0);

// The current owner of the node is MyDOMDocument
var_dump(get_class($child->ownerDocument));
// MyDOMDocument is destroyed
unset($doc);
// And new DOMDocument instance is created
var_dump(get_class($child->ownerDocument));

// Import a node from MyDOMDocument
$newdoc = new MyOtherDOMDocument;
$child = $newdoc->importNode($child);

// Register custom DOMDocument
$newdoc->registerNodeClass("DOMDocument", "MyOtherDOMDocument");

var_dump(get_class($child->ownerDocument));
unset(
$doc);
// New MyOtherDOMDocument is created
var_dump(get_class($child->ownerDocument));
?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

string(13) "MyDOMDocument"
string(11) "DOMDocument"
string(18) "MyOtherDOMDocument"
string(18) "MyOtherDOMDocument"

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #4 Custom objects are transient

ЗастСрСТСння

Objects of the registered node classes are transient, i.e. they are destroyed when they are no longer referenced from PHP code, and recreated when being retrieved again. That implies that custom property values will be lost after recreation.

<?php
class MyDOMElement extends DOMElement
{
public
$myProp = 'default value';
}

$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement', 'MyDOMElement');

$node = $doc->createElement('a');
$node->myProp = 'modified value';
$doc->appendChild($node);

echo
$doc->childNodes[0]->myProp, PHP_EOL;
unset(
$node);
echo
$doc->childNodes[0]->myProp, PHP_EOL;
?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

modified value
default value
οΌ‹add a note

User Contributed Notes 2 notes

up
4
crh3675 at gmail dot com ΒΆ
16 years ago
Creating innerHTML and outerHTML

<?php

class DOMHTMLElement extends DOMElement
{
    function __construct() { parent::__construct();} 
    
    public function innerHTML()
    {
        $doc = new DOMDocument();
      foreach ($this->childNodes as $child){
          $doc->appendChild($doc->importNode($child, true));
        }
        $content = $doc->saveHTML();
        return $content;
    }
    
    public function outerHTML()
    {
        $doc = new DOMDocument();
        $doc->appendChild($doc->importNode($this, true));
        $content = $doc->saveHTML();
        return $content;
    }
}

$dom = DOMDocument::loadHTMLFile($file);
$dom->registerNodeClass('DOMElement','DOMHTMLElement');
            
if($dom)
{
    $xpath = new DOMXpath($dom);    
    $regions = $xpath->query("//*[contains(@class, 'editable')]");    
    $content = '';
    
    foreach($regions as $region){
        $content .= $region->outerHTML();
    }    
    
    return $content;
    
}else{                
    throw new Exception('Cannot parse HTML.  Please verify the syntax is correct.');
}
?>
up
1
arnold at adaniels dot nl ΒΆ
16 years ago
Note than save and saveXML are not affected by __toString().