spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / php4xml / chap10 / 3 To page 1To page 2To page 3current pageTo page 5To page 6To page 7
[previous] [next]

Professional PHP4 XML, Chapter 10: Putting It Together

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Using DOM To Query an XML Document

Using DOM to query an XML document is rather weird. Once we parse the document we obtain the DOM tree. Then we'll have to parse the tree to perform the query, and we don't have any useful querying feature in the DOM objects that may lead to such a strategy. We won't say that we can't use DOM to query a document, but we'll say that such a solution is uncommon.

If we do decide to use DOM for our query we can use the following code:

<?php
function GetContent($domnode) 
{
    $content = '';
    foreach ($domnode->child_nodes() as $child) {
        if ($child->node_type() == XML_TEXT_NODE) {
            $content .= $child->content;
        }
    }
    return $content;
}

//$doc = xmldocfile("cars.xml");
$doc = domxml_open_file("cars.xml");  

$root = $doc->document_element();
foreach ($root->child_nodes() as $element) {
    if ($element->node_type() == XML_ELEMENT_NODE and 
                                 $element->tagname() == "car") {
        $cond1 = false;
        $cond2 = false;
        foreach ($element->child_nodes() as $car_element) {
            if ($car_element->node_type() == XML_ELEMENT_NODE and 
                                        $car_element->tagname() == "type") {
                if (($type = getContent($car_element)) == "F1") {
                    $cond1 = true;
                }
            }
            if ($car_element->node_type() == XML_ELEMENT_NODE and 
                                    $car_element->tagname() == "maxspeed") {
                if (($maxspeed = getContent($car_element))>300) {
                    $cond2 = true;
                }
            }
            if ($car_element->node_type() == XML_ELEMENT_NODE and 
                                       $car_element->tagname() == "brand") {
                $brand = getContent($car_element);
            }
            if ($car_element->node_type() == XML_ELEMENT_NODE and 
                                       $car_element->tagname() == "model") {
                $model = getContent($car_element);
            }
        }
        if ($cond1 && $cond2) {
            print ("Brand: $brand Model:$model<br />");
        }
    }
}
?>

The code shows a typical approach to using DOM for querying. Traversing the DOM tree and finding the nodes that match some conditions is not that simple, and it isn't short either.


home / programming / php / php4xml / chap10 / 3 To page 1To page 2To page 3current pageTo page 5To page 6To page 7
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: August 26, 2002
Revised: August 26, 2002

URL: http://webreference.com/programming/php/php4xml/chap10/3/4.html