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 Specified Elements in XML Document by Tag Name Using XPATH and PHP

This script will List Specified Elements in XML Document by Tag Name Using XPATH and PHP. In this case, we've used a sample file that is a website PAD file which we generate with PADGen.

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 a few tasks, first with DOM only and no XPath. Then we do the same thing using XPath. The getElementsByTagName() method seems more straightforward for this task of listing Specified Elements in XML Document by Tag Name. But keep in mind that XPath can do a lot that DOMDocument objects alone could never do. First the non-XPath version:

The new DOMDocument object is created so we can use the Document Object Model to get info from the file. We load in the XML file with the load method. Then we use the getElementsByTagName() method and ->item(0)->nodeValue to get various tag contents as 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), which we then proceed to do.

Now the XPath version: A new DOMDocument object is created because for XPATH use, you have to create a DomDocument object.
The $dom->load('http://www.theliquidateher.com/pad_file.xml') code loads $dom as it gets a PAD file's contents into the DOM object. Next we use $xpath = new DOMXPath($dom) to create a DOMXPath object with the file contents inside. Next we define the $info array. It is not needed, but it's a convenient place to store XML document info if you need to. Now we perform an XPath Query going after all elements with File_Info as the parent tag (which is why there is /* at the end of the XPath expression in the parameter of the query). The results are a DOMNodeList we can—and do—loop through, in this case putting the node values into the $info array as well as echoing each result. Then we get a single element, using the // XPath syntax to depict a desire to select nodes in the document that match the selection no matter where they are. We get the node value of the results of the query and echo it to the screen.

As you will see in List Urls in XML Sitemap by Tag Name Using XPATH and PHP, you can also loop through results you 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.

For the DOM-only version, 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.

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 below, forget the loop and just get the node values of four different tags found in the file. (Although we could have used the method that our XPATH example did: going after all elements with File_Info as the parent tag; but since there were only three tags with that parent, the way we illustrated was fine.) This is good if there are no tags with the same tag name or few child tags under any one parent tag, as just discussed. But, as in List Urls in XML Sitemap by Tag Name Using XPATH and PHP, it is often wise to loop through elements with a certain tag name. This is especially great if there are many elements with the same tag name.

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.


$doc = new DOMDocument;
$fileinfo1 = $doc->getElementsByTagName('File_Size_Bytes')->item(0)->nodeValue;
$fileinfo2 = $doc->getElementsByTagName('File_Size_K')->item(0)->nodeValue;
$fileinfo3 = $doc->getElementsByTagName('File_Size_MB')->item(0)->nodeValue;
$description = $doc->getElementsByTagName('Char_Desc_2000')->item(0)->nodeValue;

echo $fileinfo1."<BR>";
echo $fileinfo2."<BR>";
echo $fileinfo3."<BR><BR>";
echo $description."<BR><BR>";

$dom = new DOMDocument;
$xpath = new DOMXPath($dom);
$info = array();
$infoNodes = $xpath->query('//File_Info/*');
for($i=0;$i<$infoNodes->length;$i++) {
$info[$i] = $infoNodes->item($i)->nodeValue;echo $info[$i]."<BR>";}
echo "<BR>";
echo $xpath->query('//Char_Desc_2000')->item(0)->nodeValue;