jump to navigation

PHP and XML just an introduction September 9, 2010

Posted by Tournas Dimitrios in PHP.

This tutorial should summarize the possibilities of XML handling by PHP. After short introduction what XML can be used for, we’ll move to the PHP world and show how this web oriented programming language supports XML data format.

I personaly don’t know anybody who doesn’t know RSS. But to be sure: RSS is an abbreviation for Real Simple Syndication and it’s one of the most widespread content publication technologies. Why am I talking about RSS if post title indicates something about PHP and XML? Well, as most of you probably know, RSS uses the XML standard to carry data. For those, who would like to refresh what XML is about, please navigate yourselves here. And those who don’t know what PHP is, please navigate here. Well, now we can move forward to the main topic of this tutorial post.

PHP offers several forms of XML maninupation. You can choose from them in accordance with your preferences and extension accessibilities. I’m writing this because not all extensions must be present or enabled on the system you are using. In the case of required extension (or its part – e.g. some function) absence, the PHP will probably let you know about it by returning error message. If you are admin of the system where PHP runs, there is no problem to adjust the PHP directives, load necessary extension (if compiled already) and continue in work. But if the PHP is not under your control, you have to ask an administrator for an assistance.

Well, let’s suppose that PHP is running and all necessary extensions are successfully loaded, thus available. From this point the situation is more than simple. Why? Because PHP has really extensive support of XML manipulation. There’s just up to you, what’s more friendly to you to use.

PHP XML Manipulation Extensions :

* DOM – Document Object Model | manual
The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.
* DOM XML (PHP4) | manual
This extension allows you to operate on an XML document with the DOM API for PHP4.
* libxml | manual
These functions/constants are available since PHP 5.1.0 and if you have compiled one of the extensions based on libxml, like DOM, SimpleXML and XSLT.
* qtdom | manual
This extension has been moved to the PECL repository and is no longer bundled with PHP as of PHP 5.0.0.
* SDO – Service Data Objects | manual
Service Data Objects (SDOs) enable PHP applications to work with data from different sources (like a database query, an XML file, and a spreadsheet) using a single interface.
* SDO-DAS-Relational – SDO Relational Data Access Service | manual
The job of the Relational DAS is to move data between the application and a relational database. In order to do this it needs to be told the mapping between the database entities – tables, columns, primary keys and foreign keys – and the elements of the SDO model – types, properties, containment relationships and so on.
* SDO DAS XML – SDO XML Data Access Service | manual
The job of the XML DAS is to move data between the application and an XML data source, which can be either a file or a URL.
* Simple XML | manual
The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
* XML Parser | manual
This PHP extension implements support for James Clark’s expat in PHP. This toolkit lets you parse, but not validate, XML documents.
* XML Reader | manual
The XML Reader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.
* XML Writer | manual
This extension represents a writer that provides a non-cached, forward-only means of generating streams or files containing XML data.
* XSL | manual
The XSL extension implements the XSL standard, performing XSLT transformations using the libxslt library.
* XSLT (PHP4) | manual
This PHP extension provides a processor independent API to XSLT transformations. Currently this extension only supports the Sablotron library from the Ginger Alliance.

As you can see from the stated above, there is no problem manipulate XML by PHP. OK, but this was just useless theory so far… How can we use these extensions to get requested data from an XML file? Well, the answer is right here. Take a look at the PHP source code below I’ve pasted from my PHP IDE. By this example I tried to explain you how to use possibilities of PHP XML extensions, in this case the SimpleXML one. By copying and pasting this source code into a .php file you’ll immediately have very basic RSS reader of a blog. Don’t believe? Try and enjoy!


* This script demonstrates PHP manipulation by SimpleXML extension.
* For better understanding, simplicity and cleanliness, this code
* is NOT treated for errors. Use it on your own risk.

* Leave PHP display errors, if any.
* Display errors for debug only, turn it off in normal operation.
ini_set("display_errors", "on");

* Enter the feed URL.
$feed = "http://www.some-web-tha-tsupports-rss.com/wp-feed.php";

* Load whole XML file into SimpleXMLElement Object.
$xml = simplexml_load_file($feed);

* Uncomment following line to see the raw output.
//echo '
<pre>'; print_r($xml); echo '</pre>

* Format presentable output from SimpleXMLElement Object.
* For better understanding uncomment the line of code above
* to see the raw output, if it's not obvious what comes where from.

$s1 = 'style="font-size:1.7em"';
$s2 = 'style="text-transform:uppercase;font-size:1.2em"';

$header  = '<strong>s1 . '>' . $xml->channel->title . '</strong>
$header .= 's2 . '>' . $xml->channel->description . '
$header .= '<br />';

echo $header;

foreach ($xml->channel->item as $item)
 $output  = '<a href="' . $item->link . '">' . $item->title . '</a><br />';
 $output .= 'Published on ' . $item->pubDate . '
 $output .= $item->description . '<br />';
 $output .= '<br />';

 echo $output;

I hope you’ve been successfull in practice of stated above simple feed reader and the comments of source code were explanatory enough to understand the basics of XML manipulation by PHP. If not, you are welcome to post your questions in comments and I’m sure that either me or some other post reader will try answer you.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s