WebReference.com - Part 3 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (3/7)
Professional PHP4 XML, Chapter 10: Putting It Together
If our data model is based on XML we'll be querying documents very often. Some examples of the need to query XML are:
- We have an XML file describing products and we want to find a price
- We have an XML file with a shopping cart and we want to calculate the total price
- We have an XML file with a log and we want to find some statistic
- We have an XML file for cars and we want to find cars below 20,000 USD, and with three doors
Querying an XML document means that we should traverse the XML document looking for particular information; what we do with the information once it is found is beyond the scope of the problem.
Here we'll present a simple example to check how to solve the same problem using different approaches. In this case our example will show an XML file describing cars. This is the XML document:
<cars> <car> <type>F1</type> <brand>Ferrari</brand> <model>X1</model> <maxspeed>305</maxspeed> <hp>800</hp> </car> <car> <type>F1</type> <brand>Williams</brand> <model>W89</model> <maxspeed>298</maxspeed> <hp>1000</hp> </car>
<car> <type>F1</type> <brand>Brabham</brand> <model>B01</model> <maxspeed>311</maxspeed> <hp>1020</hp> </car> </cars>
And the query will be: "Find brand and model of F1 cars that can run at more than 300 kmh".
We have the following options to query an XML document:
Created: August 26, 2002
Revised: August 26, 2002