alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow PHP Oracle arrow Creating XML with the DOM PHP Extension
Site Search


Creating XML with the DOM PHP Extension

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.

  1. <?php
  2.    //File: DOM.php
  3.    if(!$rsConnection = oci_connect('hr', 'hr', '//localhost/orcl')) {
  4.       $err = oci_error();
  5.       trigger_error('Could not establish a connection: ' .
  6.             $err['message'], E_USER_ERROR);
  7.    };
  8.    $dept_id = 90;
  9.    $query = "SELECT employee_id, last_name, salary FROM employees
  10.          WHERE department_id = :deptid";
  11.    $stmt = oci_parse($rsConnection,$query);
  12.    oci_bind_by_name($stmt, ':deptid', $dept_id);
  13.    if (!oci_execute($stmt)) {
  14.     $err = oci_error($stmt);
  15.     trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
  16.    }
  17.    $dom = new DOMDocument('1.0', 'UTF-8');
  18.    $root = $dom->createElement('EMPLOYEES', '');
  19.    $root = $dom->appendChild($root);
  20.    while ($row = oci_fetch_assoc($stmt)) {
  21.       $emp = $dom->createElement('EMPLOYEE', '');
  22.       $emp = $root->appendChild($emp);
  23.       $emp->setAttribute('id', $row['EMPLOYEE_ID']);
  24.       $ename = $dom->createElement('ENAME', $row['LAST_NAME']);
  25.       $ename = $emp->appendChild($ename);
  26.       $salary = $dom->createElement('SALARY', $row['SALARY']);
  27.       $salary = $emp->appendChild($salary);
  28.    }
  29.    echo $dom->saveXML();
  30.    $dom->save("employees.xml");
  31. ?>
 

 

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 save method to the same folder where the script source fi le 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:

	King24000Kochhar17000De Haan17000 

However, if you look at the source, you should see the following XML document:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.    <EMPLOYEES>
  3.       <EMPLOYEE id="100">
  4.       <ENAME>King</ENAME>
  5.       <SALARY>24000</SALARY>
  6.    </EMPLOYEE>
  7.    <EMPLOYEE id="101">
  8.       <ENAME>Kochhar</ENAME>
  9.       <SALARY>17000</SALARY>
  10.    </EMPLOYEE>
  11.    <EMPLOYEE id="102">
  12.       <ENAME>De Haan</ENAME>
  13.       <SALARY>17000</SALARY>
  14.       </EMPLOYEE>
  15.    </EMPLOYEES>
 

 

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.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape