Friday, October 16, 2020

PHP XML Dom Parser



Hello folks! welcome to another section of my tutorial on PHP. In this tutorial guide, we are going to be studying about PHP XML Dom Parser.

A Html Dom parser is written in PHP5.x versions. Dom parser is very good at dealing with xml as well as Html. Dom parser travel based on tree based and before access to the data, it will load in the data into dom object and it will then update the data to the web browser.

Example

The below example shows how to get access to the html data in web browser -

<?php 
   $html = ' 
      <head> 
         <title>Web Design Tutorialz</title>
      </head> 
   
      <body> 
         <h2>Course details</h2> 
      
         <table border = "0"> 
            <tbody> 
               <tr> 
                  <td>Android</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>Hadoop</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>HTML</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>Web technologies</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>Graphic</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>Writer</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
         
               <tr> 
                  <td>Writer</td> 
                  <td>Kennedy</td> 
                  <td>Web Design Tutorialz Team</td> 
               </tr> 
            </tbody> 
         </table> 
      </body> 
   </html> 
   '; 
   /*** a new dom object ***/ 
   $dom = new domDocument; 
   
   /*** load the html into the object ***/ 
   $dom->loadHTML($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->item(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 />'; 
   }
?> 

Output

When the above code is executed, it will produce the following result -


RECOMMENDED: PHP XML SAX Parser

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial guide, we are going to be discussing about PHP Frameworks.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.