HomePHP Handling HTML Strings and Files with the DOM XML Extension in PHP 5
Handling HTML Strings and Files with the DOM XML Extension in PHP 5
The DOM XML extension has a few additional methods that can be used to process HTML files and strings, at least at a pretty basic level. Thus, this fifth part of the series will be entirely focused on explaining how to work with these methods. I will include some illustrative code samples.
Working on XML documents with PHP can be quite a challenging task, since this process often involves reading data from remote files, parsing specific nodes, inserting and removing elements from the document tree, and so forth. However, if you’re just about to tackle the development of a web application that requires thorough processing of XML data, there’s no need to start pulling your hair because PHP comes equipped with a helpful extension, called DOM XML, which can be used to parse XML documents in all sorts of clever ways using the API of the Document Object Model.
Understanding how to use the methods and properties provided by this library requires a little effort from you, despite its fairly easy learning curve. Therefore, if you’re looking for a comprehensive guide on how to get the most out of this powerful XML-related PHP extension, then you should take a closer look at this article series. In doing so, you’ll hopefully learn a few useful things, such as creating XML documents from scratch, appending, copying, and removing nodes, and even processing HTML strings by way of their DOM representation. What else can you ask for?
Now that you’re well aware of the topics that are treated in this series of articles, let me quickly review the items that were discussed in the last tutorial. This way you’ll be able to have clearer idea about how they can be linked with the ones I plan to cover in this article.
Throughout the course of the preceding tutorial I explained how to access multiple nodes of a specific XML document using the “getElementsByTagName()” method, which presents practically the same functionality as the one utilized to parse web pages with JavaScript. In addition, I demonstrated how to read XML data from an existing text file via the “load()” method. And finally, I demonstrated how to read XML strings by way of a similar method, called “loadXML()”.
As you can see, the DOM XML extension has plenty of options when it comes to moving portions of an XML document (or even the entire document) from one place to another. This is certainly a process that can be performed with minor hassles by utilizing the intuitive DOM API mentioned in the beginning.
So far, so good. At this moment, I'm assuming that you’ve acquired a considerable background in performing some basic operations on simple XML documents through the functionality offered by the DOM XML extension. But what if I tell you that this library permits you to work with data formatted in plain HTML?
Let’s not waste any more time in preliminaries and continue learning how to work with HTML and the DOM XML library. Let’s get started!