DOMDocument::createElement

(PHP 5, PHP 7)
Create new element node
public DOMElement DOMDocument::createElement ( string $name [, string $value ] )

This function creates a new instance of class DOMElement. This node will not show up in the document unless it is inserted with (e.g.) DOMNode::appendChild().

Parameters:
name

The tag name of the element.

value

The value of the element. By default, an empty element will be created. The value can also be set later with DOMElement::$nodeValue.

Returns:

Returns a new instance of class DOMElement or FALSE if an error occurred.

Exception:

Raised if name contains an invalid character.

Notes:

The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.

Examples:
Creating a new element and inserting it as root
1
2
3
4
5
6
7
8
9
10
11
<?php
 
$dom new DOMDocument('1.0''utf-8');
 
$element $dom->createElement('test''This is the root element!');
 
// We insert the new element as root (child of the document)
$dom->appendChild($element);
 
echo $dom->saveXML();
?>

The above example will output:

<?xml version="1.0" encoding="utf-8"?>
<test>This is the root element!</test>
See also:

DOMNode::appendChild() -

DOMDocument::createAttribute() -

DOMDocument::createAttributeNS() -

DOMDocument::createCDATASection() -

DOMDocument::createComment() -

DOMDocument::createDocumentFragment() -

DOMDocument::createElementNS() -

DOMDocument::createEntityReference() -

DOMDocument::createProcessingInstruction() -

DOMDocument::createTextNode() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.