Contents
DOM Function
DOMAttr
__construct()
Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
Example:
$doc = new DOMDocument();
$attr = new DOMAttr("id", "unique-value");
echo $attr->name; // Output: id
echo $attr->value; // Output: unique-value
isId()
Description: Checks if this attribute is an ID attribute.
Example:
$doc = new DOMDocument();
$element = $doc->createElement("example");
$attr = $doc->createAttribute("id");
$attr->value = "unique-id";
$element->setAttributeNode($attr);
echo $attr->isId() ? "true" : "false";
// Output: true (if the attribute is declared as an ID in a DTD)
DOMCdataSection
__construct()
Description: Constructs a new DOMCdataSection object. This is used to create a CDATA section within the document.
Example:
$doc = new DOMDocument();
$cdata = $doc->createCDATASection("This is a CDATA section.");
$doc->appendChild($cdata);
echo $doc->saveXML();
// Output:
DOMDocument
__construct()
Description: Constructs a new DOMAttr object. This is used to create an attribute node with a specified name and value.
Example:
$doc = new DOMDocument();
$attr = new DOMAttr("id", "unique-value");
echo $attr->name; // Output: id
echo $attr->value; // Output: unique-value
createAttribute()
Description: Creates a new attribute node with the specified name.
Example:
$doc = new DOMDocument();
$element = $doc->createElement("example");
$attr = $doc->createAttribute("id");
$attr->value = "unique-id";
$element->setAttributeNode($attr);
echo $attr->isId() ? "true" : "false";
// Output: true (if the attribute is declared as an ID in a DTD)
count()
Description: Get the count of elements present in a PriorityQueue
instance.
Example:
$doc = new DOMDocument();
$attr = $doc->createAttribute("class");
$attr->value = "my-class";
echo $attr->name; // Output: class
createAttributeNS()
Description: Creates a new attribute with the specified namespace URI and qualified name.
Example:
$doc = new DOMDocument();
$attr = $doc->createAttributeNS("http://example.com", "ex:attr");
$attr->value = "value";
echo $attr->name; // Output: ex:attr
createCDATASection()
Description: Creates a new CDATA section.
Example:
$doc = new DOMDocument();
$cdata = $doc->createCDATASection("CDATA Content");
$doc->appendChild($cdata);
echo $doc->saveXML();
// Output:
createComment()
Description: Creates a new comment node.
Example:
$doc = new DOMDocument();
$comment = $doc->createComment("This is a comment.");
$doc->appendChild($comment);
echo $doc->saveXML();
// Output:
createDocumentFragment()
Description: Creates an empty DOMDocumentFragment object.
Example:
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendXML("Content ");
$doc->appendChild($fragment);
echo $doc->saveXML();
// Output: Content
createElement()
Description: Creates a new element with the given tag name.
Example:
$doc = new DOMDocument();
$element = $doc->createElement("example", "This is content.");
$doc->appendChild($element);
echo $doc->saveXML();
// Output: This is content.
createElementNS()
Description: Creates a new element with a namespace URI and qualified name.
Example:
$doc = new DOMDocument();
$element = $doc->createElementNS("http://example.com", "ex:example", "Content");
$doc->appendChild($element);
echo $doc->saveXML();
// Output: Content
createEntityReference()
Description: Creates a new entity reference.
Example:
$doc = new DOMDocument();
$entity = $doc->createEntityReference("copy");
$doc->appendChild($entity);
echo $doc->saveXML();
// Output: ©
createProcessingInstruction()
Description: Creates a new processing instruction.
Example:
$doc = new DOMDocument();
$pi = $doc->createProcessingInstruction("php", "echo 'Hello';");
$doc->appendChild($pi);
echo $doc->saveXML();
// Output:
createTextNode()
Description: Creates a new text node.
Example:
$doc = new DOMDocument();
$text = $doc->createTextNode("This is text.");
$doc->appendChild($text);
echo $doc->saveXML();
// Output: This is text.
getElementById()
Description: Returns an element by its ID.
Example:
$doc = new DOMDocument();
$doc->loadXML('Content ');
echo $doc->getElementById("unique")->nodeValue;
// Output: Content
getElementsByTagName()
Description: Returns a list of elements by tag name.
Example:
$doc = new DOMDocument();
$doc->loadXML("One Two ");
$elements = $doc->getElementsByTagName("child");
foreach ($elements as $element) {
echo $element->nodeValue . "\n";
}
// Output: One Two
getElementsByTagNameNS()
Description: Returns a list of elements by tag name within a namespace.
Example:
$doc = new DOMDocument();
$doc->loadXML('Content ');
$elements = $doc->getElementsByTagNameNS("http://example.com", "child");
foreach ($elements as $element) {
echo $element->nodeValue . "\n";
}
// Output: Content
importNode()
Description: Imports a node from another document.
Example:Â
$doc1 = new DOMDocument();
$doc1->loadXML("Content ");
$doc2 = new DOMDocument();
$imported = $doc2->importNode($doc1->documentElement, true);
$doc2->appendChild($imported);
echo $doc2->saveXML();
// Output: Content
load()
Description: Loads an XML document from a file.
Example:
$doc = new DOMDocument();
$doc->load("file.xml");
echo $doc->saveXML();
DOMDocumentFragment
DOMDocumentFragment appendXML() Function
Description: Find and return an element in the PriorityQueue by a condition.
Example:
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$xml = "- First Item
- Second Item
";
$fragment->appendXML($xml);
$root = $doc->createElement("root");
$doc->appendChild($root);
$root->appendChild($fragment);
echo $doc->saveXML();
// Output:
// - First Item
- Second Item
__construct()
Description: Initializes a new DOMElement object.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example", "Hello World");
echo $element->nodeName;
// Output: example
DOMElement
Description: Initializes a new DOMElement object.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$reversed = array_reverse($pq->toArray());
print_r($reversed);
// Output: Array ( [0] => One [1] => Two [2] => Three )
getAttribute()
Description: Retrieves the value of an attribute by name.
Example:
$doc = new DOMDocument();
$element = $doc->createElement('example');
$element->setAttribute('id', '123');
echo $element->getAttribute('id');
// Output: 123
getAttributeNode()
Description: Retrieves the attribute node by name.
Example:
$doc = new DOMDocument();
$element = $doc->createElement('example');
$element->setAttribute('id', '123');
$attrNode = $element->getAttributeNode('id');
echo $attrNode->name . ' = ' . $attrNode->value;
// Output: id = 123
getAttributeNodeNS()
Description: Retrieves an attribute node by its namespace URI and name.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Another One", 1);
$priorityToExtract = 1;
$extracted = array_filter($pq->toArray(), function ($item) use ($priorityToExtract) {
return $item[1] === $priorityToExtract;
});
print_r($extracted);
// Output: Array ( [0] => One [1] => Another One )
getAttributeNS()
Description: Retrieves the value of an attribute by namespace URI and name.
Example:
$doc = new DOMDocument();
$element = $doc->createElement('example');
$element->setAttributeNS('http://example.com/ns', 'ex:id', '789');
echo $element->getAttributeNS('http://example.com/ns', 'id');
// Output: 789
DOMEntityReference
__construct()
Description: Initializes a new DOMEntityReference object, which represents an entity reference in the DOM. Entity references are placeholders for entities that are replaced by their defined content in the XML or HTML document.
Example:
$doc = new DOMDocument();
$entityRef = $doc->createEntityReference('copy');
$doc->appendChild($entityRef);
echo $doc->saveXML();
// Output: ©
DOMImplementation
__construct()
Description: Initializes a new DOMImplementation object, which provides methods for operations independent of the document instance.
Example:
$domImpl = new DOMImplementation();
var_dump($domImpl instanceof DOMImplementation);
// Output: bool(true)
createDocument()
Description: Creates a new DOMDocument object with an optional namespace URI, qualified name, and document type.
Example:
$domImpl = new DOMImplementation();
$doc = $domImpl->createDocument("http://www.example.com", "example:root");
echo $doc->saveXML();
// Output:
createDocumentType()
Description: Creates a new DOMDocumentType object. It is used to define the document type declaration.
Example:
$domImpl = new DOMImplementation();
$doctype = $domImpl->createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
echo $doctype->name; // Output: html
hasFeature()
Description: Checks if the DOMImplementation object supports a specific feature and version.
Example:
$doc = new DOMDocument();
$attr = $doc->createAttribute('id');
$attr->value = '456';
$element = $doc->createElement('example');
$element->setAttributeNode($attr);
echo $element->getAttribute('id');
// Output: 456
setAttributeNodeNS()
Description: Adds a new attribute node with a namespace to the element.
Example:
$domImpl = new DOMImplementation();
if ($domImpl->hasFeature("XML", "1.0")) {
echo "Feature XML 1.0 is supported.";
} else {
echo "Feature not supported.";
}
// Output: Feature XML 1.0 is supported.
DOMNamedNodeMap
count()
Description: Returns the number of items in the DOMNamedNodeMap.
Example:
$dom = new DOMDocument();
$dom->loadXML(' ');
$attributes = $dom->documentElement->attributes;
echo $attributes->count();
// Output: 3
getNamedItem()
Description: Retrieves a node specified by name from the DOMNamedNodeMap.
Example:
$dom = new DOMDocument();
$dom->loadXML(' ');
$attributes = $dom->documentElement->attributes;
$attr = $attributes->getNamedItem('attr1');
echo $attr->nodeValue;
// Output: value1
item()
Description: Returns a node at the specified index in the DOMNamedNodeMap.
Example:
$dom = new DOMDocument();
$dom->loadXML(' ');
$attributes = $dom->documentElement->attributes;
$attr = $attributes->item(0);
echo $attr->nodeName . ": " . $attr->nodeValue;
// Output: attr1: value1
DOMNode
appendChild()
Description: Appends a new child node to the DOM node. This function adds the node to the list of children of the current node.
Example:
$doc = new DOMDocument();
$element = $doc->createElement('example');
$element->setAttributeNS('http://example.com/ns', 'ex:id', 'unique');
$element->setIdAttributeNS('http://example.com/ns', 'id', true);
echo $doc->getElementById('unique')->nodeName;
// Output: example
C14N()
Description: Canonicalizes the XML of the current node and returns it as a string.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example", "Hello World!");
$dom->appendChild($element);
$element->C14NFile("example.xml");
// The file "example.xml" will contain the canonicalized XML.
cloneNode()
Description: Creates a duplicate of the current node. It can clone the node with or without its children.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example", "Clone me!");
$dom->appendChild($element);
$clone = $element->cloneNode(true);
$dom->appendChild($clone);
echo $dom->saveXML();
// Output: Clone me! Clone me!
getLineNo()
Description: Returns the line number of the current node within the document.
Example:
$dom = new DOMDocument();
$dom->loadXML('Content ');
$child = $dom->getElementsByTagName("child")->item(0);
echo $child->getLineNo();
// Output: 2 (This depends on the line where the element is located)
getNodePath()
Description: Returns the XPath of the node.
Example:
$dom = new DOMDocument();
$dom->loadXML('Value ');
$child = $dom->getElementsByTagName("child")->item(0);
echo $child->getNodePath();
// Output: /root/parent/child
hasAttributes()
Description: Checks if the current node has attributes.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example");
$element->setAttribute("attr", "value");
echo $element->hasAttributes() ? "Has attributes" : "No attributes";
// Output: Has attributes
hasChildNodes()
Description: Checks if the current node has child nodes.
Example:
$dom = new DOMDocument();
$parent = $dom->createElement("parent");
$child = $dom->createElement("child");
$parent->appendChild($child);
echo $parent->hasChildNodes() ? "Has child nodes" : "No child nodes";
// Output: Has child nodes
insertBefore()
Description: Inserts a new node before a specified reference node.
Example:
$dom = new DOMDocument();
$parent = $dom->createElement("parent");
$child1 = $dom->createElement("child1");
$child2 = $dom->createElement("child2");
$parent->appendChild($child1);
$parent->insertBefore($child2, $child1);
echo $dom->saveXML();
// Output:
isDefaultNamespace()
Description: Checks if the given namespace URI is the default namespace for the current node.
Example:
$dom = new DOMDocument();
$element = $dom->createElementNS("http://www.example.com", "example:element");
echo $element->isDefaultNamespace("http://www.example.com") ? "Default namespace" : "Not default namespace";
// Output: Default namespace
isSameNode()
Description: Checks if two nodes are the same node.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example");
$clone = $element->cloneNode();
echo $element->isSameNode($clone) ? "Same node" : "Different nodes";
// Output: Different nodes
isSupported()
Description: Checks if the current node supports the specified feature and version.
Example:
$dom = new DOMDocument();
$element = $dom->createElement("example");
echo $element->isSupported("core", "1.0") ? "Supported" : "Not supported";
// Output: Supported
lookupNamespaceUri()
Description: Looks up the URI associated with a namespace prefix.
Example:
$dom = new DOMDocument();
$element = $dom->createElementNS("http://www.example.com", "example:element");
echo $element->lookupNamespaceUri("example");
// Output: http://www.example.com
lookupPrefix()
Description: Looks up the prefix associated with a given namespace URI.
Example:
$dom = new DOMDocument();
$element = $dom->createElementNS("http://www.example.com", "example:element");
echo $element->lookupPrefix("http://www.example.com");
// Output: example
normalize()
Description: Normalizes the current node by merging adjacent text nodes and removing empty text nodes.
Example:
$dom = new DOMDocument();
$textNode = $dom->createTextNode("Hello");
$space = $dom->createTextNode(" ");
$otherTextNode = $dom->createTextNode("World!");
$element = $dom->createElement("example");
$element->appendChild($textNode);
$element->appendChild($space);
$element->appendChild($otherTextNode);
$element->normalize();
echo $dom->saveXML();
// Output: Hello World!
removeChild()
Description: Removes a child node from the current node.
Example:
$dom = new DOMDocument();
$parent = $dom->createElement("parent");
$child = $dom->createElement("child");
$parent->appendChild($child);
$parent->removeChild($child);
echo $dom->saveXML();
// Output:
removeChild()
Description: Removes a child node from the current node.
Example:
$dom = new DOMDocument();
$parent = $dom->createElement("parent");
$child = $dom->createElement("child");
$parent->appendChild($child);
$parent->removeChild($child);
echo $dom->saveXML();
// Output:
replaceChild()
Description: Replaces a child node with another node.
Example:
$dom = new DOMDocument();
$parent = $dom->createElement("parent");
$child1 = $dom->createElement("child1");
$child2 = $dom->createElement("child2");
$parent->appendChild($child1);
$parent->replaceChild($child2, $child1);
echo $dom->saveXML();
// Output:
DOMProcessingInstruction
DOMProcessingInstruction __construct()
Description: Constructs a new processing instruction. This is typically used to add instructions for applications like XML stylesheets.
Example:
$dom = new DOMDocument();
$pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
$dom->appendChild($pi);
echo $dom->saveXML();
// Output:
DOMProcessingInstruction data()
Description: Retrieves or sets the content of the processing instruction.
Example:
$dom = new DOMDocument();
$pi = $dom->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");
$dom->appendChild($pi);
echo $pi->data;
// Output: type="text/xsl" href="style.xsl"
DOMProcessingInstruction target()
Description: Retrieves the target of the processing instruction, such as the application it is intended for.
Example:
DOMText
PHP DOMText __construct() Function
Description: Constructs a new DOMText
object. A DOMText
node is used to represent text within an element or attribute in the DOM.
Example:
$dom = new DOMDocument();
$textNode = new DOMText("Hello, World!");
$dom->appendChild($textNode);
echo $dom->saveXML();
// Output: Hello, World!
PHP DOMText isElementContentWhitespace() Function
Description: Checks if the text node contains only whitespace and is part of the element’s content.
Example:
$dom = new DOMDocument();
$dom->loadXML(" Content ");
$textNode = $dom->documentElement->firstChild;
echo $textNode->isElementContentWhitespace() ? "Yes" : "No";
// Output: Yes
PHP DOMText isWhitespaceInElementContent() Function
Description: An alias for isElementContentWhitespace()
. Checks if the text node contains only whitespace and is part of the element’s content.
Example:
$dom = new DOMDocument();
$dom->loadXML(" Content ");
$textNode = $dom->documentElement->firstChild;
echo $textNode->isWhitespaceInElementContent() ? "Yes" : "No";
// Output: Yes
PHP DOMText splitText() Function
Description: Splits a text node into two at the specified offset. The original node contains the text before the offset, and a new node is created with the text after the offset.
Example:
$dom = new DOMDocument();
$textNode = $dom->createTextNode("Hello, World!");
$dom->appendChild($textNode);
$newNode = $textNode->splitText(7);
echo $textNode->wholeText . "\n"; // Output: Hello,
echo $newNode->wholeText; // Output: World!
DOMXPath
PHP DOMXPath __construct() Function
Description: Creates a new DOMXPath
object for querying and evaluating XPath expressions on a DOMDocument
.
$dom = new DOMDocument();
$dom->loadXML("- Value
");
$xpath = new DOMXPath($dom);
echo "DOMXPath object created.";
PHP DOMXPath evaluate() Function
Description: Evaluates an XPath expression and returns the result. The result can be a DOMNodeList
, a string, a number, or a boolean, depending on the expression.
Example:
$dom = new DOMDocument();
$dom->loadXML("- Value
");
$xpath = new DOMXPath($dom);
$result = $xpath->evaluate("string(/root/item)");
echo $result; // Output: Value
PHP DOMXPath query() Function
Description: Executes an XPath query and returns a DOMNodeList
containing all nodes matching the expression.
Example:
$dom = new DOMDocument();
$dom->loadXML("- Value1
- Value2
");
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//item");
foreach ($nodes as $node) {
echo $node->nodeValue . "\n";
}
// Output:
// Value1
// Value2
PHP DOMXPath registerNamespace() Function
Description: Registers a namespace with a prefix for use in XPath queries.
Example:
$dom = new DOMDocument();
$dom->loadXML('Value ');
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns", "http://example.com");
$result = $xpath->evaluate("string(//ns:item)");
echo $result; // Output: Value
PHP DOMXPath registerPhpFunctions() Function
Description: Enables PHP functions to be used within XPath expressions. By default, PHP functions are not available in XPath queries.
Example:
$dom = new DOMDocument();
$dom->loadXML('- Value
');
$xpath = new DOMXPath($dom);
$xpath->registerPhpFunctions();
$result = $xpath->evaluate("php:function('strtoupper', /root/item)");
echo $result; // Output: VALUE
DOM Functions
PHP dom_import_simplexml() Function
Description:The dom_import_simplexml() function converts a SimpleXMLElement object into a DOMElement object, allowing you to work with the node using the DOM extension methods.
Example:
// Create a SimpleXMLElement object
$simpleXml = simplexml_load_string("- Value
");
// Import the SimpleXMLElement into a DOMElement
$domElement = dom_import_simplexml($simpleXml);
if ($domElement) {
echo $domElement->nodeName; // Output: root
}