PHP | DOMDocument registerNodeClass() Function
Last Updated :
20 Feb, 2020
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
class myElement extends DOMElement
{
public function appendElement( $name )
{
return $this ->appendChild( new myElement( $name ));
}
}
class myDocument extends DOMDocument {
public function setRoot( $name ) {
return $this ->appendChild( new myElement( $name ));
}
}
$doc = new myDocument();
$doc ->registerNodeClass( 'DOMElement' , 'myElement' );
$root = $doc ->setRoot( 'div' );
$child = $root ->appendElement( 'div' );
$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
class myElement extends DOMElement {
public function getData() {
return $this ->nodeValue;
}
}
$doc = new DOMDocument;
$doc ->loadXML(
"<root><div><h1>This is my heading</h1></div></root>" );
$doc ->registerNodeClass( "DOMElement" , "myElement" );
$element = $doc ->getElementsByTagName( "h1" )->item(0);
echo $element ->getData();
?>
|
Output:
This is my heading
Reference: https://www.php.net/manual/en/domdocument.registernodeclass.php
Share your thoughts in the comments
Please Login to comment...