class DOMDocument extends DOMNode implements DOMParentNode (View source)

The DOMDocument class represents an entire HTML or XML document; serves as the root of the document tree.

Constants

DOCUMENT_POSITION_DISCONNECTED Since: 8.4

DOCUMENT_POSITION_PRECEDING Since: 8.4

DOCUMENT_POSITION_FOLLOWING Since: 8.4

DOCUMENT_POSITION_CONTAINS Since: 8.4

DOCUMENT_POSITION_CONTAINED_BY Since: 8.4

DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC Since: 8.4

Properties

string $nodeName from  DOMNode
string|null $nodeValue from  DOMNode
int $nodeType from  DOMNode
DOMNode|null $parentNode from  DOMNode
DOMNode> $childNodes from  DOMNode
DOMNode|null $firstChild from  DOMNode
DOMNode|null $lastChild from  DOMNode
DOMNode|null $previousSibling from  DOMNode
DOMNode|null $nextSibling from  DOMNode
DOMAttr>|null $attributes from  DOMNode
DOMDocument|null $ownerDocument from  DOMNode
string|null $namespaceURI from  DOMNode
string|null $prefix from  DOMNode
string|null $localName from  DOMNode
string|null $baseURI from  DOMNode
string $textContent from  DOMNode
bool $isConnected from  DOMNode
DOMElement|null $parentElement from  DOMNode
string|null $actualEncoding
DOMConfiguration $config
DOMDocumentType $doctype
DOMElement $documentElement
string|null $documentURI
string|null $encoding
bool $formatOutput
DOMImplementation $implementation
bool $preserveWhiteSpace
bool $recover
bool $resolveExternals
bool $standalone
bool $strictErrorChecking
bool $substituteEntities
bool $validateOnParse
string $version
string|null $xmlEncoding
bool $xmlStandalone
string|null $xmlVersion
$childElementCount
$lastElementChild
$firstElementChild

Methods

insertBefore(DOMNode $node, null|DOMNode $child = null)

Adds a new child before a reference node

from  DOMNode
DOMNode|false
replaceChild(DOMNode $node, DOMNode $child)

Replaces a child

from  DOMNode
removeChild(DOMNode $child)

Removes child from list of children

from  DOMNode
appendChild(DOMNode $node)

Adds new child at the end of the children

from  DOMNode
bool
hasChildNodes()

Checks if node has children

from  DOMNode
cloneNode(bool $deep = false)

Clones a node

from  DOMNode
void
normalize()

Normalizes the node

from  DOMNode
bool
isSupported(string $feature, string $version)

Checks if feature is supported for specified version

from  DOMNode
bool
hasAttributes()

Checks if node has attributes

from  DOMNode
int
compareDocumentPosition(DOMNode $other)

No description

from  DOMNode
bool
isSameNode(DOMNode $otherNode)

Indicates if two nodes are the same node

from  DOMNode
string|null
lookupPrefix(string $namespace)

Gets the namespace prefix of the node based on the namespace URI

from  DOMNode
bool
isDefaultNamespace(string $namespace)

Checks if the specified namespaceURI is the default namespace or not

from  DOMNode
string|null
lookupNamespaceURI(string|null $prefix)

Gets the namespace URI of the node based on the prefix

from  DOMNode
string|null
lookupNamespaceUri(string|null $prefix)

Gets the namespace URI of the node based on the prefix

from  DOMNode
bool
isEqualNode($otherNode)

No description

from  DOMNode
getFeature($feature, $version)

No description

from  DOMNode
setUserData($key, $data, $handler)

No description

from  DOMNode
getUserData($key)

No description

from  DOMNode
string|null
getNodePath()

Gets an XPath location path for the node

from  DOMNode
int
getLineNo()

Get line number for a node

from  DOMNode
string|false
C14N(bool $exclusive = false, bool $withComments = false, null|array $xpath = null, null|array $nsPrefixes = null)

Canonicalize nodes to a string

from  DOMNode
int|false
C14NFile(string $uri, bool $exclusive = false, bool $withComments = false, null|array $xpath = null, null|array $nsPrefixes = null)

Canonicalize nodes to a file.

from  DOMNode
bool
contains(DOMNode|DOMNameSpaceNode|null $other)

No description

from  DOMNode
getRootNode(array|null $options = null)

No description

from  DOMNode
array
__sleep()

No description

from  DOMNode
void
__wakeup()

No description

from  DOMNode
DOMElement|false
createElement(string $localName, string $value = '')

Create new element node

createDocumentFragment()

Create new document fragment

createTextNode(string $data)

Create new text node

createComment(string $data)

Create new comment node

createCDATASection(string $data)

Create new cdata node

createProcessingInstruction(string $target, string $data = '')

Creates new PI node

DOMAttr|false
createAttribute(string $localName)

Create new attribute

createEntityReference(string $name)

Create new entity reference node

getElementsByTagName(string $qualifiedName)

Searches for all elements with given tag name

DOMNode|false
importNode(DOMNode $node, bool $deep = false)

Import node into current document

DOMElement|false
createElementNS(string|null $namespace, string $qualifiedName, string $value = '')

Create new element node with an associated namespace

DOMAttr|false
createAttributeNS(string|null $namespace, string $qualifiedName)

Create new attribute node with an associated namespace

getElementsByTagNameNS(string $namespace, string $localName)

Searches for all elements with given tag name in specified namespace

DOMElement|null
getElementById(string $elementId)

Searches for an element with a certain id

adoptNode(DOMNode $node)

No description

void
append(DOMNode|string|null ...$nodes)

Appends one or many nodes to the list of children behind the last child node.

void
prepend(DOMNode|string|null ...$nodes)

Prepends one or many nodes to the list of children before the first child node.

void
replaceChildren(...$nodes)

No description

void
normalizeDocument()

Normalizes the document

renameNode(DOMNode $node, $namespace, $qualifiedName)

No description

load(string $filename, int $options = 0)

Load XML from a file

int|false
save(string $filename, int $options = null)

Dumps the internal XML tree back into a file

loadXML(string $source, int $options = 0)

Load XML from a string

string|false
saveXML(null|DOMNode $node = null, int $options = 0)

Dumps the internal XML tree back into a string

__construct(string $version = '1.0', string $encoding = '')

Creates a new DOMDocument object

bool
validate()

Validates the document based on its DTD

int|false
xinclude(int $options = 0)

Substitutes XIncludes in a DOMDocument Object

loadHTML(string $source, int $options = 0)

Load HTML from a string

loadHTMLFile(string $filename, int $options = 0)

Load HTML from a file

string|false
saveHTML(DOMNode $node = null)

Dumps the internal document into a string using HTML formatting

int|false
saveHTMLFile(string $filename)

Dumps the internal document into a file using HTML formatting

bool
schemaValidate(string $filename, int $options = null)

Validates a document based on a schema

bool
schemaValidateSource(string $source, int $flags)

Validates a document based on a schema

bool
relaxNGValidate(string $filename)

Performs relaxNG validation on the document

bool
relaxNGValidateSource(string $source)

Performs relaxNG validation on the document

bool
registerNodeClass(string $baseClass, string $extendedClass)

Register extended class used to create base node type

Details

DOMNode insertBefore(DOMNode $node, null|DOMNode $child = null)

Adds a new child before a reference node

Parameters

DOMNode $node

The new node.

null|DOMNode $child

[optional]

The reference node. If not supplied, newnode is appended to the children.

Return Value

DOMNode

The inserted node.

DOMNode|false replaceChild(DOMNode $node, DOMNode $child)

Replaces a child

Parameters

DOMNode $node

The new node. It must be a member of the target document, i.e. created by one of the DOMDocument->createXXX() methods or imported in the document by .

DOMNode $child

The old node.

Return Value

DOMNode|false

The old node or false if an error occur.

DOMNode removeChild(DOMNode $child)

Removes child from list of children

Parameters

DOMNode $child

The removed child.

Return Value

DOMNode

If the child could be removed the functions returns the old child.

DOMNode appendChild(DOMNode $node)

Adds new child at the end of the children

Parameters

DOMNode $node

The appended child.

Return Value

DOMNode

The node added.

bool hasChildNodes()

Checks if node has children

Return Value

bool

true on success or false on failure.

DOMNode cloneNode(bool $deep = false)

Clones a node

Parameters

bool $deep

Indicates whether to copy all descendant nodes. This parameter is defaulted to false.

Return Value

DOMNode

The cloned node.

void normalize()

Normalizes the node

Return Value

void

bool isSupported(string $feature, string $version)

Checks if feature is supported for specified version

Parameters

string $feature

The feature to test. See the example of DOMImplementation::hasFeature for a list of features.

string $version

The version number of the feature to test.

Return Value

bool

true on success or false on failure.

bool hasAttributes()

Checks if node has attributes

Return Value

bool

true on success or false on failure.

int compareDocumentPosition(DOMNode $other)

No description

Parameters

DOMNode $other

Return Value

int

bool isSameNode(DOMNode $otherNode)

Indicates if two nodes are the same node

Parameters

DOMNode $otherNode

The compared node.

Return Value

bool

true on success or false on failure.

string|null lookupPrefix(string $namespace)

Gets the namespace prefix of the node based on the namespace URI

Parameters

string $namespace

The namespace URI.

Return Value

string|null

The prefix of the namespace.

bool isDefaultNamespace(string $namespace)

Checks if the specified namespaceURI is the default namespace or not

Parameters

string $namespace

The namespace URI to look for.

Return Value

bool

Return true if namespaceURI is the default namespace, false otherwise.

string|null lookupNamespaceURI(string|null $prefix)

Gets the namespace URI of the node based on the prefix

Parameters

string|null $prefix

The prefix of the namespace.

Return Value

string|null

The namespace URI of the node.

string|null lookupNamespaceUri(string|null $prefix)

Gets the namespace URI of the node based on the prefix

Parameters

string|null $prefix

The prefix of the namespace.

Return Value

string|null

The namespace URI of the node.

bool isEqualNode($otherNode)

No description

Parameters

$otherNode

Return Value

bool

getFeature($feature, $version)

No description

Parameters

$feature
$version

setUserData($key, $data, $handler)

No description

Parameters

$key
$data
$handler

getUserData($key)

No description

Parameters

$key

string|null getNodePath()

Gets an XPath location path for the node

Return Value

string|null

the XPath, or NULL in case of an error.

int getLineNo()

Get line number for a node

Return Value

int

Always returns the line number where the node was defined in.

string|false C14N(bool $exclusive = false, bool $withComments = false, null|array $xpath = null, null|array $nsPrefixes = null)

Canonicalize nodes to a string

Parameters

bool $exclusive

[optional] Enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.

bool $withComments

[optional] Retain comments in output.

null|array $xpath

[optional] An array of xpaths to filter the nodes by.

null|array $nsPrefixes

[optional] An array of namespace prefixes to filter the nodes by.

Return Value

string|false

Canonicalized nodes as a string or FALSE on failure

int|false C14NFile(string $uri, bool $exclusive = false, bool $withComments = false, null|array $xpath = null, null|array $nsPrefixes = null)

Canonicalize nodes to a file.

Parameters

string $uri

Number of bytes written or FALSE on failure

bool $exclusive

[optional] Enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.

bool $withComments

[optional] Retain comments in output.

null|array $xpath

[optional] An array of xpaths to filter the nodes by.

null|array $nsPrefixes

[optional] An array of namespace prefixes to filter the nodes by.

Return Value

int|false

Number of bytes written or FALSE on failure

bool contains(DOMNode|DOMNameSpaceNode|null $other)

Since: 8.3

No description

Parameters

DOMNode|DOMNameSpaceNode|null $other

Return Value

bool

DOMNode getRootNode(array|null $options = null)

Since: 8.3

No description

Parameters

array|null $options

Return Value

DOMNode

array __sleep()

Since: 8.1

No description

Return Value

array

void __wakeup()

Since: 8.1

No description

Return Value

void

DOMElement|false createElement(string $localName, string $value = '')

Create new element node

Parameters

string $localName

The tag name of the element.

string $value

[optional]

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

Return Value

DOMElement|false

A new instance of class DOMElement or false if an error occurred.

Exceptions

DOMException

DOMDocumentFragment createDocumentFragment()

Create new document fragment

Return Value

DOMDocumentFragment

The new DOMDocumentFragment or false if an error occurred.

DOMText createTextNode(string $data)

Create new text node

Parameters

string $data

The content of the text.

Return Value

DOMText

The new DOMText or false if an error occurred.

DOMComment createComment(string $data)

Create new comment node

Parameters

string $data

The content of the comment.

Return Value

DOMComment

The new DOMComment or false if an error occurred.

DOMCDATASection|false createCDATASection(string $data)

Create new cdata node

Parameters

string $data

The content of the cdata.

Return Value

DOMCDATASection|false

The new DOMCDATASection or false if an error occurred.

DOMProcessingInstruction|false createProcessingInstruction(string $target, string $data = '')

Creates new PI node

Parameters

string $target

The target of the processing instruction.

string $data

The content of the processing instruction.

Return Value

DOMProcessingInstruction|false

The new DOMProcessingInstruction or false if an error occurred.

DOMAttr|false createAttribute(string $localName)

Create new attribute

Parameters

string $localName

The name of the attribute.

Return Value

DOMAttr|false

The new DOMAttr or false if an error occurred.

Exceptions

DOMException

DOMEntityReference|false createEntityReference(string $name)

Create new entity reference node

Parameters

string $name

The content of the entity reference, e.g. the entity reference minus the leading & and the trailing ; characters.

Return Value

DOMEntityReference|false

The new DOMEntityReference or false if an error occurred.

DOMNodeList getElementsByTagName(string $qualifiedName)

Searches for all elements with given tag name

Parameters

string $qualifiedName

The name of the tag to match on. The special value * matches all tags.

Return Value

DOMNodeList

A new DOMNodeList object containing all the matched elements.

DOMNode|false importNode(DOMNode $node, bool $deep = false)

Import node into current document

Parameters

DOMNode $node

The node to import.

bool $deep

If set to true, this method will recursively import the subtree under the importedNode.

To copy the nodes attributes deep needs to be set to true

Return Value

DOMNode|false

The copied node or false, if it cannot be copied.

DOMElement|false createElementNS(string|null $namespace, string $qualifiedName, string $value = '')

Create new element node with an associated namespace

Parameters

string|null $namespace

The URI of the namespace.

string $qualifiedName

The qualified name of the element, as prefix:tagname.

string $value

[optional]

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

Return Value

DOMElement|false

The new DOMElement or false if an error occurred.

Exceptions

DOMException

DOMAttr|false createAttributeNS(string|null $namespace, string $qualifiedName)

Create new attribute node with an associated namespace

Parameters

string|null $namespace

The URI of the namespace.

string $qualifiedName

The tag name and prefix of the attribute, as prefix:tagname.

Return Value

DOMAttr|false

The new DOMAttr or false if an error occurred.

Exceptions

DOMException

DOMNodeList getElementsByTagNameNS(string $namespace, string $localName)

Searches for all elements with given tag name in specified namespace

Parameters

string $namespace

The namespace URI of the elements to match on. The special value * matches all namespaces.

string $localName

The local name of the elements to match on. The special value * matches all local names.

Return Value

DOMNodeList

A new DOMNodeList object containing all the matched elements.

DOMElement|null getElementById(string $elementId)

Searches for an element with a certain id

Parameters

string $elementId

The unique id value for an element.

Return Value

DOMElement|null

The DOMElement or null if the element is not found.

adoptNode(DOMNode $node)

No description

Parameters

DOMNode $node

void append(DOMNode|string|null ...$nodes)

Appends one or many nodes to the list of children behind the last child node.

Parameters

DOMNode|string|null ...$nodes

Return Value

void

void prepend(DOMNode|string|null ...$nodes)

Prepends one or many nodes to the list of children before the first child node.

Parameters

DOMNode|string|null ...$nodes

Return Value

void

void replaceChildren(...$nodes)

Since: 8.3 {@inheritDoc}

No description

Parameters

...$nodes

Return Value

void

void normalizeDocument()

Normalizes the document

Return Value

void

renameNode(DOMNode $node, $namespace, $qualifiedName)

No description

Parameters

DOMNode $node
$namespace
$qualifiedName

DOMDocument|bool load(string $filename, int $options = 0)

Load XML from a file

Parameters

string $filename

The path to the XML document.

int $options

[optional]

Bitwise OR of the libxml option constants.

Return Value

DOMDocument|bool

true on success or false on failure. Prior to PHP 8.3 if called statically, returns a DOMDocument and issues E_STRICT warning.

int|false save(string $filename, int $options = null)

Dumps the internal XML tree back into a file

Parameters

string $filename

The path to the saved XML document.

int $options

[optional]

Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.

Return Value

int|false

the number of bytes written or false if an error occurred.

DOMDocument|bool loadXML(string $source, int $options = 0)

Load XML from a string

Parameters

string $source

The string containing the XML.

int $options

[optional]

Bitwise OR of the libxml option constants.

Return Value

DOMDocument|bool

true on success or false on failure. Prior to PHP 8.3 if called statically, returns a DOMDocument and issues E_STRICT warning.

string|false saveXML(null|DOMNode $node = null, int $options = 0)

Dumps the internal XML tree back into a string

Parameters

null|DOMNode $node

[optional]

Use this parameter to output only a specific node without XML declaration rather than the entire document.

int $options

[optional]

Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.

Return Value

string|false

the XML, or false if an error occurred.

__construct(string $version = '1.0', string $encoding = '')

Creates a new DOMDocument object

Parameters

string $version

[optional] The version number of the document as part of the XML declaration.

string $encoding

[optional] The encoding of the document as part of the XML declaration.

bool validate()

Validates the document based on its DTD

Return Value

bool

true on success or false on failure. If the document have no DTD attached, this method will return false.

int|false xinclude(int $options = 0)

Substitutes XIncludes in a DOMDocument Object

Parameters

int $options

[optional]

libxml parameters. Available since PHP 5.1.0 and Libxml 2.6.7.

Return Value

int|false

the number of XIncludes in the document.

DOMDocument|bool loadHTML(string $source, int $options = 0)

Load HTML from a string

Parameters

string $source

The HTML string.

int $options

[optional]

Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

Return Value

DOMDocument|bool

true on success or false on failure. Prior to PHP 8.3 if called statically, returns a DOMDocument and issues E_STRICT warning.

DOMDocument|bool loadHTMLFile(string $filename, int $options = 0)

Load HTML from a file

Parameters

string $filename

The path to the HTML file.

int $options

[optional]

Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

Return Value

DOMDocument|bool

true on success or false on failure. Prior to PHP 8.3 if called statically, returns a DOMDocument and issues E_STRICT warning.

string|false saveHTML(DOMNode $node = null)

Dumps the internal document into a string using HTML formatting

Parameters

DOMNode $node

[optional] parameter to output a subset of the document.

Return Value

string|false

The HTML, or false if an error occurred.

int|false saveHTMLFile(string $filename)

Dumps the internal document into a file using HTML formatting

Parameters

string $filename

The path to the saved HTML document.

Return Value

int|false

the number of bytes written or false if an error occurred.

bool schemaValidate(string $filename, int $options = null)

Validates a document based on a schema

Parameters

string $filename

The path to the schema.

int $options

[optional]

Bitwise OR of the libxml option constants.

Return Value

bool

true on success or false on failure.

bool schemaValidateSource(string $source, int $flags)

Validates a document based on a schema

Parameters

string $source

A string containing the schema.

int $flags

[optional]

A bitmask of Libxml schema validation flags. Currently the only supported value is LIBXML_SCHEMA_CREATE. Available since PHP 5.5.2 and Libxml 2.6.14.

Return Value

bool

true on success or false on failure.

bool relaxNGValidate(string $filename)

Performs relaxNG validation on the document

Parameters

string $filename

The RNG file.

Return Value

bool

true on success or false on failure.

bool relaxNGValidateSource(string $source)

Performs relaxNG validation on the document

Parameters

string $source

A string containing the RNG schema.

Return Value

bool

true on success or false on failure.

bool registerNodeClass(string $baseClass, string $extendedClass)

Register extended class used to create base node type

Parameters

string $baseClass

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

string $extendedClass

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

Return Value

bool

true on success or false on failure.