PHP - DOM Parser Example
PHP - DOM Parser Example
The DOM extension in PHP comes with extensive functionality with which we can perform various operations on XML and HTML documents. We can dynamically construct a DOM object, load a DOM document from a HTML file or a string with HTML tag tree. We can also save the DOM document to a XML file, or extract the DOM tree from a XML document.
The DOMDocument class is one the most important classes defined in the DOM extension.
$obj = new DOMDocument($version = "1.0", $encoding = "")It represents an entire HTML or XML document; serves as the root of the document tree. The DOMDocument class includes definitions of a number of static methods, some of which are introduced here β
Sr.NoMethods & Description1createElementCreate new element node2createAttributeCreate new attribute3createTextNodeCreate new text node4getElementByIdSearches for an element with a certain id5getElementsByTagNameSearches for all elements with given local tag name6loadLoad XML from a file7loadHTMLLoad HTML from a string8loadHTMLFileLoad HTML from a file9loadXMLLoad XML from a string10saveDumps the internal XML tree back into a file11saveHTMLDumps the internal document into a string using HTML formatting12saveHTMLFileDumps the internal document into a file using HTML formatting13saveXMLDumps the internal XML tree back into a string
Example
Let us use the following HTML file for this example β
We shall now extract the Document Object Model from the above HTML file by calling the loadHTMLFile() method in the following PHP code β
<?php
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
$dom->loadHTMLFile("hello.html");
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');
/*** get all rows from the table ***/
$rows = $tables[0]->getElementsByTagName('tr');
/*** loop over the table rows ***/
foreach ($rows as $row) {
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
/*** echo the values ***/
echo 'Designation: '.$cols->item(0)->nodeValue.'<br />';
echo 'Manager: '.$cols->item(1)->nodeValue.'<br />';
echo 'Team: '.$cols->item(2)->nodeValue;
echo '<hr />';
}
?>It will produce the followingoutputβ
Designation: Android
Manager: Gopal
Team: Sairam
________________________________________
Designation: Hadoop
Manager: Gopal
Team: Satish
________________________________________
Designation: HTML
Manager: Gopal
Team: Raju
________________________________________
Designation: Web technologies
Manager: Gopal
Team: Javed
________________________________________
Designation: Graphic
Manager: Gopal
Team: Satish
________________________________________
Designation: Writer
Manager: Kiran
Team: Amith
________________________________________
Designation: Writer
Manager: Kiran
Team: Vineeth
________________________________________