HomePHP Working with Attributes and CDATA Sections with the DOM XML Extension
Working with Attributes and CDATA Sections with the DOM XML Extension
Undeniably, one of the most remarkable strengths of PHP is its robust set of extensions. They provide developers with the right tools to build a huge variety of web applications with minor hassles. And this characteristic is particularly evident when it comes to working with XML documents, since PHP comes packaged with a bunch of powerful libraries that allow it to handle XML data in all sorts of clever ways.
A good example of this is the popular “SimpleXML” extension, which comes in handy when performing the most common operation required to work with XML documents -- using an approachable, easy-to-learn API. You might think that this library is the only one bundled with PHP that can be used to handle XML data, but this is completely erroneous.
There’s a PHP library that permits you to work with XML documents by using the DOM API. Does this name ring any bells for you? Yes, you’re right! Fortunately, PHP also provides programmers with the helpful DOM XML extension, which, as its name suggests, can be quite useful when processing XML documents via the Document Object Model.
Basically, this implies that you’ll be able to build XML documents from scratch, create and append new nodes, remove existing ones, work with custom attributes, etc., by using the methods offered by the DOM API, which include the popular “createElement(),” appendChild(),” and “getElementById().”
And speaking of the numerous features offered by this library, in the first article of the series I explained how to use some of its simplest methods to perform certain basic tasks on XML data, such as building new documents, creating elements and inserting nodes, and finally echoing the whole output to a browser.
However, this is merely a humble beginning, since the DOM XML library comes equipped with many other methods that can be useful for adding custom attributes to the existing nodes of a given XML document, and creating new CDATA sections, among other things. Therefore, assuming that all these topics are really interesting to you, in this second part of the series I’ll be discussing them in detail and logically accompanying the corresponding theoretical concepts with illustrative hands-on examples.
Does all this material sound good to you? Great! Let’s continue this journey now!