As I stated in the previous section, the DOM XML library comes equipped with another useful method that can be used to read the contents of a specified HTML file, which can then be easily processed later on. The method that performs such a useful task is called “loadHTMLFile()” and can be implemented in the following way: (definition of ‘sample_file.htm’ file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>This is a sample HTML file</title> </head> <body> <p>This is a sample HTML file</p> </body> </html> // example on loading HTML from an existing file using the 'loadHTMLFile()' method $dom=new DOMDocument(); // load HTML from existing file $dom->loadHTMLFile('sample_file.htm'); echo $dom->saveHTML(); /* displays the following <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>This is a sample HTML file</title> </head> <body> <p>This is a sample HTML file</p> </body> </html> */ As you can see, I first defined a primitive HTML file, called “sample_file.htm”, and then coded a simple script that is responsible for using the aforementioned “loadHTMLFile()” method to read the corresponding file content. In the end, the script finishes its execution by displaying this content on the browser. After looking at the previous hands-on example, you’ll have to agree with me that using the DOM XML API to read the content from a few specified HTML files is actually a no-brainer process that can be tackled with minor troubles. All right, you learned a few additional methods that come packaged with the DOM XML extension aimed at handling HTML in different ways. Of course, the functionality of these methods is rather limited, but they do perform decently when it comes to working with basic HTML strings and files. However, the DOM XML library provides you with yet another HTML-related method that can potentially be useful, particularly in those cases where you need to build an HTML file from its corresponding DOM representation. This sounds interesting, right? Assuming that this topic has caught your attention, in the last section of this tutorial, I’m going to show you how to create HTML files using the DOM API. To see how this will be achieved, please jump ahead and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|