XML-Enabled Applications
[next]
XML-Enabled Applications
[This is an escerpt from the book, PHP Oracle Web Development: Data processing, Security, Caching, XML, Web Services, and Ajax, by Yuli Vasiliev. Published by Packt Publishing Ltd., 2007
Both PHP and Oracle provide comprehensive support for XML and XML-related technologies. Practically, this means you can perform any XML processing either with PHP or inside an Oracle database. While PHP allows you to construct and transform XML by using either PHP's XML extensions or PEAR XML packages, Oracle provides the Oracle XML DB, which has a wide set of XML features that can be used to efficiently store, retrieve, update, as well as transform XML data and generate it from relational data.
This chapter explains how to effectively use XML techniques and technologies available in PHP and Oracle when building XML-enabled PHP/Oracle applications. Specifically, you will see how to:
|
Processing XML in PHP/Oracle Applications
As mentioned, there are two alternatives when it comes to performing XML processing in your PHP/Oracle application. You can perform any required XML processing using either PHP's XML extensions (or PEAR XML packages) or Oracle's XML features.
In the following sections, you will learn how to construct XML from relational data using the XML capabilities of both PHP and Oracle.
Processing XML Data with PHP
PHP provides three general extensions allowing you to work with XML. These extensions are listed in the following table:
In practice, you should choose the extension that best suits the needs of your applications. For example, the XML extension implementing the SAX model can be very efficient when it comes to parsing large XML documents from which you only want to extract useful information. In contrast, the DOM extension comes in handy when you need to generate XML documents or modify existing ones. With the SimpleXML extension, XML documents are turned into data structures that can be then iterated like regular PHP arrays and objects, thus providing the most natural way for PHP developers to access data.
Since the Document Object Model (DOM) is best used for solving complex tasks, the following sections demonstrate how to use DOM extension APIs to generate, query, and manipulate XML documents in PHP.
[ Admittedly, the Document Object Model is widely used in web development. Web browsers, for example, use the DOM to represent web pages they display to the users. In Chapter 10 AJAX-Based Applications, you will learn techniques to access and manipulate the DOM tree of a web page sent to the browser by your application, thus allowing you to produce more interactive and responsive PHP/Oracle solutions. ]
Creating XML with the DOM PHP Extension
In fact, the PHP DOM extension is a set of classes that can be used to generate, access, and manipulate XML data. The DOM.php script defined in the following listing shows how to generate an XML document based on the result set retrieved from the database.
To figure out what happens when you run the DOM.php
script, let's take a closer look at this code.
You start by connecting to the database as hr/hr. Then, you define a query, which, when issued, retrieves some information about the employees working in the department whose ID is 90.
After the query is executed, you create a new DOM document that will be used to wrap the retrieved result set in XML format. You start generating a new DOM document by creating the root element and then appending it to the DOM tree.
In the next step you create the nodes of the DOM document based on the data retrieved from the database. For this, you fetch the data from the result set in a loop, creating the document structure.
In this example, you simply display the generated XML document using the saveXML method of the DOMDocument object and then save it to disk with the savemethod to the same folder where the script source file resides. However, in a real-world situation, you probably would continue processing this XML document, producing a result XML document that could then, for example, be sent to a web service or published as an RSS feed.
When you run the DOM.php
script discussed here, you probably will see the following string in your browser:
However, if you look at the source, you should see the following XML document:
After running the DOM.php
script, the employees.xml
file containing the document shown in the listing should appear in the folder where the script source file resides.
[next]
URL: