We now have a youtube channel. Subscribe!

PHP XML Dom Parser

PHP XML Dom Parser


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP XML Dom Parser.

Html Dom parser is written in PHP version 5.x. Dom parser is good at dealing with xml and also Html. Dom parser travels base 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 browser.

Example

The following example below 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 -



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

Feel free to ask your questions where necessary and we 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain