PHP | DOMDocument registerNodeClass() Function

The DOMDocument::registerNodeClass() function is an inbuilt function in PHP which is used to register extended class used to create base node type.
Syntax:
bool DOMDocument::registerNodeClass( string $baseclass,
                              string $extendedclass )
Parameters: This function accept two parameters as mentioned above and described below:
- $baseclass: It specifies the DOM class that you want to extend.
- $extendedclass: It specifies the extended class name.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the DOMDocument::registerNodeClass() function in PHP:
Program 1: In this program we will create a HTML div element with CSS properties using classes.
| <?php  Â// Create a class myElement classmyElement extendsDOMElement {     // Create a custom function to     // append the element     publicfunctionappendElement($name)     {         return$this->appendChild(newmyElement($name));     } }  Â// Create a class myDocoment classmyDocument extendsDOMDocument {  Â    // Create a custom function to set the root     publicfunctionsetRoot($name) {         return$this->appendChild(newmyElement($name));     } }  Â// Create a instance of above class $doc= newmyDocument();  Â// Register the node class $doc->registerNodeClass('DOMElement', 'myElement');  Â// Use setRoot created in myDocument class $root= $doc->setRoot('div');  Â// Use appendElement created in myElement $child= $root->appendElement('div');  Â// Set the attribute $child->setAttribute('style',     'background:blue; width:100px;height:100px');  Âecho$doc->saveXML(); ?>  | 
Output:
Program 2: In this program we will get the text content of a tag using classes.
| <?php  ÂclassmyElement extendsDOMElement {  Â    // Create a custom function to     // get the value of node     publicfunctiongetData() {         return$this->nodeValue;     } }  Â// Create a new DOMDocument $doc= newDOMDocument;  Â// Load the XML $doc->loadXML( "<root><div><h1>This is my heading</h1></div></root>");  Â// Register the node class $doc->registerNodeClass("DOMElement", "myElement");  Â// Get the element $element= $doc->getElementsByTagName("h1")->item(0);  Â// Use the custom created getData() function echo$element->getData(); ?>  | 
Output:
This is my heading
Reference: https://www.php.net/manual/en/domdocument.registernodeclass.php
 
				 
					



