O'Reilly logo

XML Parsing with PHP by John M. Stokes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Advanced DOM

What This Chapter is Not

This chapter isn’t a comprehensive look at every advanced feature in the DOM library. The DOM library is enormous. We are going to examine DOM’s approach to some advanced XML features. A more comprehensive reference to the DOM library can be found in Appendix B.

XPath

We’ve discussed XPath several times, but DOM is where it really becomes essential.

Let’s start by loading our library file.

$DOMdoc = new DOMDocument();
$DOMdoc->load('library.xml');

To perform XPath queries with DOM, we must create an XPath object using a DOMDocument object.

$xpath = new DOMXpath($DOMdoc);

Remember our query for all book nodes from Chapter 2? We can retrieve all book nodes with the absolute XPath expression /library/book ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required