In PHP, the DOMDocument class provides a powerful tool for working with HTML and XML documents. With it, we can efficiently parse, modify, and generate document content. This article will guide you through using the DOMDocument class to load, manipulate, and save HTML and XML documents.
When using the DOMDocument class, the first step is to instantiate a DOMDocument object so that you can operate on the document content.
$dom = new DOMDocument();
Once instantiated, we can load an HTML or XML document for further manipulation.
DOMDocument offers several methods for loading HTML or XML documents, including:
Here’s an example of loading an HTML document from a string:
$html = "<html><body><p>Hello, World!</p></body></html>";
$dom->loadHTML($html);
You can also load an HTML document from a file:
$dom->loadHTMLFile('example.html');
Or load an XML document:
$dom->load('example.xml');
Once the document is loaded, we can use various DOMDocument methods to navigate and manipulate the content of the document. Some common methods include:
For example, to retrieve all paragraph elements:
$paragraphs = $dom->getElementsByTagName('p');
foreach ($paragraphs as $paragraph) {
echo $paragraph->nodeValue;
}
Create a new heading element and append it to the document:
$newElement = $dom->createElement('h2', 'New Heading');
$parentElement->appendChild($newElement);
Remove a node:
$parentElement->removeChild($childElement);
After manipulating the document, you can export it as either an HTML or XML document. The common methods are:
Convert the document to an HTML string:
$htmlString = $dom->saveHTML();
Save the document as an HTML file:
$dom->saveHTMLFile('output.html');
Or save it as an XML file:
$dom->save('output.xml');
The DOMDocument class in PHP provides powerful functionality for handling HTML and XML documents. With it, we can easily load, navigate, modify, and generate document content. Whether parsing an HTML page or manipulating an XML file, DOMDocument is a highly useful tool.