PHP DOM: explained and exemplified

This is guide to get started with PHP DOM or a quick reminder to those who have a little while since the last time they used it. The extended documentation is in PHP.net, but it is quite long. Here you might found a quick reference to get started in no time.

Purpose of the DOM (Docuement Object Model): It is a convention used to represent and manipulate objects in XML, XHTML and HTML documents. Parsing XML and HTML files is very useful. It allows to manipulate RSS Feeds, interact with APIs and web services through XML (e.g. Google Maps, Facebook and Twitter APIs, etc.), extract information from websites (web crawling) and more.

Getting Started

The DOM implementation in PHP have more than 15 classes! But don't get afraid, for most cases, you might just end up using these ones: DOMNode, DOMDocument, DOMNodeList and DOMElement. In the following UML class diagram of PHP's DOM you will see how these classes are related to each other and them the explanation of each one.

Fig 1. PHP DOM: UML Class Diagram

Loading and Saving DOM Documents

DOMDocument — The DOMDocument class which exteds from DOMNode. This class contains the XML (or HTML) elements and configurations. It has configurations attributes, such as format output, preserve white spaces, versions, etc.

Iterating through DOM Elements

The first thing you need to do after loading the XML that you want to process, it's to select the data that you are intereted in. To search for you data you need to iterate through the DOM elements and you need to know what methods and objects are using in this process.

Adrian Mejia is a full-stack web developer located in Boston.
Currently working at Cisco as a Software Engineer.
Adrian enjoys writing posts about programming and technology.
Also, he likes to travel ✈️ and biking 🚴‍. Find out more here.