Home      Products & Services      Contact Us      Links

WebHatchers will design & develop your site for you.

Website Menu Heaven: menus, buttons, etc.

Send us your questions.

site search by freefind

SEO, Google, Privacy
   and Anonymity
Browser Insanity
Popups and Tooltips
Free Website Search
HTML Form Creator
Buttons and Menus
Image Uploading
Website Poll
IM and Texting
   or Not MySQL
Personal Status Boards
Content Management
Article Content
   Management Systems
Website Directory
   CMS Systems
Photo Gallery CMS
Forum CMS
Blog CMS
Customer Records
   Management CMS
Address Book CMS
Private Messaging CMS
Chat Room CMS
JavaScript Charts
   and Graphs

Free Personal Status Boards (PSB™)

Free Standard Free PSB

Free PSB Pro Version

Free Social PSB

Free Social PSB Plus (with Email)

Free Business PSB

Free Business PSB Plus (with Email)

PSB demo

Social PSB demo

Business PSB demo

So what's all this PSB stuff about?

Chart comparing business status boards

PSB hosting diagram

PSB Licence Agreement

Copyright © 2002 -
MCS Investments, Inc. sitemap

PSBs, social networking, social evolution, microcommunities, personal status boards
PSBs, social networking, business personal status boards
website design, ecommerce solutions
website menus, buttons, image rotators
Ez-Architect, home design software
the magic carpet and the cement wall, children's adventure book
the squirrel valley railroad, model railroad videos, model train dvds
the deep rock railroad, model railroad videos, model train dvds

List Elements in HTML Document by Tag Name Using XPATH and PHP

This script will List Elements in HTML Document by Tag Name Using XPATH and PHP. In this case, we've used a sample file that is this HTML file, called:
html-test-table.html. Here is all it has in it:


<table border="1">
  <caption>Monthly savings</caption>


The script uses the PHP DOM extension and PHP 5. The DOM extension is enabled by default in most PHP installations, so the following should work fine—it does for us. The DOM extension allows you to operate on XML documents through the DOM API with PHP 5. It supports XPATH 1.0, which this script uses extensively. XPATH has been around awhile. What is it? XPath is a syntax for defining parts of an XML document (or an HTML or XHTML one). It uses path expressions to navigate in documents. It contains a library of standard functions.

The DOMXPath class has the DOMDocument property and several very useful methods: DOMXPath::__construct, DOMXPath::evaluate (which evaluates the given XPath expression and returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression), DOMXPath::query (which evaluates and executes the given XPath expression and returns a DOMNodeList containing all nodes matching the given XPath expression), DOMXPath::registerNamespace (which is necessary to use XPath to handle documents which have default namespaces described in the xmlns declaration which in the case of a sitemap is in the urlset tag), and DOMXPath::registerPhpFunctions. Most XML files seem to have no xmlns declaration (e.g., PAD files), therefore needing no namespace registration.

We perform the task (on an HTML file) of listing the caption tag node, then we list the td nodes. We do it using the DOMDocument method without XPath first, then we do it again with XPath. We throw in the task of listing the th nodes as well, with the XPath syntax. The getElementsByTagName() method is used for listing the caption and td tags' nodes, since using it you don't even need the XPath syntax and we felt it would be good to show both the DOM-only and XPath methods. We use XPath query to illustrate how it's done with XPath expressions and XPath queries even though getElementsByTagName() without XPath would do as well—as we illustrated. Keep in mind that XPath can do a lot that DOMDocument objects alone could never do. A non-XPath version of XML file node selection is at List Specified Elements in XML Document by Tag Name Using XPATH and PHP. An XPath version using XPath query is below after the getElementsByTagName() method without XPath.

A new DOMDocument object is created. We load in the HTML file with the loadHTMLFile method. The $dom->loadHTMLFile($html) code loads $dom as it gets our HTML file's contents into the DOM object. The getElementsByTagName method is used to get the caption node which we turn into a string variable before echoing it to the screen. Next we define the $td array. It is not needed, but it's a convenient place to store HTML document info if you need to. Now the getElementsByTagName method is used again to get the td nodes which we loop through and turn into string variables before echoing them to the screen.

To get echoable nodes, we use the length of this DOMNodeList in a for loop to loop through these nodes, getting strings we can echo by use of: ->item(0)->nodeValue. The results of our use of the getElementsByTagName method is a DOMNodeList, and we put the node values into the $td array. We need strings that we can echo since raw DOM objects do not echo until you get their value as a string since echo only outputs strings, and nodeValue gets the nodes as strings.

As you will see in List Specified Elements in XML Document by Tag Name Using XPATH and PHP, you can get tag nodes one at a time using getElementsByTagName, but this is useful only if there are a lot a unique tags with few or no children. In the script on this page, there are three different tags in the HTML file with a couple of child nodes, so we can get child nodes either one at a time or in a loop. We chose one at a time at first, then switched to looping through the DOMNodeList. In the List Urls in XML Sitemap by Tag Name Using XPATH and PHP script, we loop through results we get when using the getElementsByTagName() method, since this method returns a new instance of class DOMNodeList containing the elements with a given tag name. These are easy to loop through, as this page's script also demonstrates.

Once we are looping, we use if($i%2) for inserting a line break if $i is odd. The MOD operator lets us know if there is a remainder after the division occurs—in this case we're dividing by 2. This is all just for display purposes. But now let's look at the XPath version that gets the same results (except that we added th node retrieval just for kicks) as the DOM-only version:

The XPath version uses $xpath = new DOMXPath($doc), which creates an XPath object to use with the XPath query syntax. We first use //caption in our query parameter, with the // meaning find this node(s) wherever it is. We echo is after getting its node value. Then we do the query twice mode, using //th and //td for the query paths. Both of these queries are followed by looping through the DOMNodeList and using ->item($i)->nodeValue to get echoable values. The
if($i==1) conditional statement is used for display purposes only. Feel free to stick results into nice, neat HTML tables or MySQL databases.

For DOM-only versions using the getElementsByTagName() method, there's no need for $xpath = new DOMXPath($doc), which creates an XPath object to use with the getElementsByTagName() method, because you do not need XPath for a getElementsByTagName method. But for $xpath->query() methods, XPath is essential. Note that we did not need to deal with namespace registration or XPATH either with the getElementsByTagName() method, because no XPATH is necessarily involved, but we needed it for the XPATH versions of scripts.

If an XPATH expression or non-XPATH expression returns a node set, you will get a DOMNodeList which can be looped through to get values. In the non-XPATH version in List Specified Elements in XML Document by Tag Name Using XPATH and PHP, we simply forget the loop and just get the node values of four different tags found in the file. This is good if there are no tags with the same tag name or few child tags under any one parent tag. But, it is essential to loop through nodes when there are many elements with the same tag name, as in List Urls in XML Sitemap by Tag Name Using XPATH and PHP.

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. You can get more information on the syntax to use in XPath expressions in the W3Schools XPath expression page.


// Initializing the DOM [Document Object Model]
$dom = new DOMDocument();
$html = "html-test-table.html";
$dom->loadHTMLFile($html);//Loads the source for parsing
$caption = $dom->getElementsByTagName('caption')->item(0)->nodeValue;
echo $caption."<BR><BR>";
$nodes = $dom->getElementsByTagName("td");
$nodeListLength = $nodes->length;
for ($i = 0; $i < $nodeListLength; $i ++){
$td[$i] = $nodes->item($i)->nodeValue;}
while ($i < $nodeListLength){echo $td[$i]."<BR>";
if($i%2){echo "<BR>";}

echo "<HR>";
$doc = new DomDocument("1.0", "ISO-8859-1");
$html = "html-test-table.html";
$xpath = new DomXPath($doc);
$testNodes = $xpath->query('//caption');
echo $testNodes->item(0)->nodeValue."<BR>";
echo "<BR>";
$testNodes = $xpath->query('//th');
for($i=0;$i<$testNodes->length;$i++) {
echo $testNodes->item($i)->nodeValue."<BR>";}
echo "<BR>";
$testNodes = $xpath->query('//td');
for($i=0;$i<$testNodes->length;$i++) {
echo $testNodes->item($i)->nodeValue."<BR>";
if($i==1){echo "<BR>";}}