XML and PHP Simplified | WebReference

XML and PHP Simplified

By Leidago Noabeb


Often the Document Object Model is thought of as an abstract concept that is very difficult for beginners to deal with. With this series of articles, I will try to simplify and demystify the use and application of XML and the DOM. First, we will look at what XML is and then move on to what functions are available for use to manipulate and use XML.

What is XML?

XML is an acronym for eXtensible Markup Language; it was designed to make it easy to represent data in a structured way, almost like a database. Though it is not quite a database, it does allow for formatting and storing structured data persistently. Just because the word language appears in XML does not actually mean that XML is a language, it is better to interpret it as a specification that enables you to create your own markup language(s). It is a subset of Standard Generalized Markup Language, which is the mother of all markup languages; incidentally, it is also the parent of the more popular Hyper Text Markup Language or HTML. XML, as already stated, makes it easy to exchange information between different applications. For example, a program written in PHP will be able to process information created and stored in a XML document by another programming language such as PERL or ColdFusion. For those of you who are familiar with HTML, using XML documents should not be a problem at all, because it is similar to how documents are formatted in HTML. The main difference between the two is that the HTML specification has a fixed set of elements and attributes so you have to learn to use those if you want to write any sensible HTML document. That in itself is an advantage because it makes it easy for developers across the world to read and write HTML documents. For example if you want to make text bold in HTML you use the <b></b> or <strong></strong> tags. Any developer that is familiar with HTML will know what they are. XML on the other hand does not have any fixed elements or attributes. It allows you to create your own elements and attributes, this of course gives you the power to define your own language or to use someone else's definition. The flexibility that XML offers is what makes it so powerful not to mention useful. This is also why programs created by different applications can read and write to a XML document.

Structure of an XML Document

To create an XML document is very easy, viewing an XML document is not. Although an XML document is similar to an HTML document in the sense that it is a specification and not actually a language and also in the sense that it has elements and attributes defined as tags, it cannot be directly rendered by many browsers unless you specify formatting information in some way. We will get to this in a moment, but look at how easy it is to create a XML document:

An XML document consist of the following:

Version - Describes the version of XML that is used by this document.
Root Element - In this case, the root element is called articles. There can only be one root element per XML document.
Child element - The third line defines a child element of the root element, in this case it is called article and it has an attribute called title, which is set to "Change in Politics".

From the structure of the document above you can easily work out that the document is about articles and that the first article is titled "Change in Politics". Articles have other attributes as well, such as date that it was written, and author name. Therefore, if we want to get a fuller description for our document we can modify it in the following manner:

It is that difficult and that easy to create your own document. To summarize an XML document must have the following parts:

All XML documents must contain an xml version line. It may also contain a character encoding declaration. For example:

To make your XML document more portable or more readable by other applications, a valid XML document will contain a DTD or an XML Schema, or at least a reference to one of these. For example:

XML documents contain one or more elements; they in turn can contain more than one attribute. Elements can contain other elements or information between their starting and ending tags. For example:

XML functions in PHP

PHP being one of the most popular server side scripting languages has long being able to store, retrieve and manipulate information stored in databases. Because XML has gained so much popularity among developers, PHP started to add functions that enabled developers to manipulate XML data. Not only do these functions enable us to manipulate data contained within XML documents, it also enables us to create XML documents. Most of the functions are designed to parse XML documents. As you can imagine, to work with an XML document, these functions must be able to get at and work with the names and values of elements and attributes and any other type of XML document components. In the next couple of sections and articles, we will look at the various functions that are provided by PHP. Here are some of the XML functions that are available in PHP

xml_parser_create: This is the basic function to create an xml parser, which can then be used with the other XML functions for reading and writing data, getting errors, and a variety of other useful tasks. Use xml_parser_free() to free up the resource when done. An example use:

$xmlParser = xml_parser_create();

xml_parse_into_struct: Parse XML data into an array structure. You can use this function to take the contents of a well-formed XML file, turn it into a PHP array, and then work with the contents of the array. Below is an example use of this function:

When you run the above example, you get the following result: