Sponsored Links
PHP Oracle Tutorials
- PHP Oracle Web Development
- XML Processing in PHP and Oracle Applications
- Creating XML with the DOM PHP Extension
- Querying a DOM Document with XPath
- Transforming and Processing XML with XSLT
- Performing XML Processing inside the Database
- Moving All the XML Processing into the Database
- Performing XSLT Transformations inside the Database
- Using Oracle Database for Storing, Modifying, and Retrieving XML Data
- Using XMLType for Handling XML Data in the Database
- Using XML Schemas
- Retrieving XML Data
- Using XMLType Views
- Performing DML Operations on XML Schema Based XMLType Views
- Using Oracle XML DB Repository
- Accessing Repository Resources with SQL
- Taking Advantage of Standard Internet Protocols
- Querying Data with Oracle XQuery
- Breaking up XML into Relational Data
Tutorials
PHP OracleCreating 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.
- <?php
- //File: DOM.php
- if(!$rsConnection = oci_connect('hr', 'hr', '//localhost/orcl')) {
- $err = oci_error();
- $err['message'], E_USER_ERROR);
- };
- $dept_id = 90;
- $query = "SELECT employee_id, last_name, salary FROM employees
- WHERE department_id = :deptid";
- $stmt = oci_parse($rsConnection,$query);
- oci_bind_by_name($stmt, ':deptid', $dept_id);
- if (!oci_execute($stmt)) {
- $err = oci_error($stmt);
- }
- $dom = new DOMDocument('1.0', 'UTF-8');
- $root = $dom->createElement('EMPLOYEES', '');
- $root = $dom->appendChild($root);
- while ($row = oci_fetch_assoc($stmt)) {
- $emp = $dom->createElement('EMPLOYEE', '');
- $emp = $root->appendChild($emp);
- $emp->setAttribute('id', $row['EMPLOYEE_ID']);
- $ename = $dom->createElement('ENAME', $row['LAST_NAME']);
- $ename = $emp->appendChild($ename);
- $salary = $dom->createElement('SALARY', $row['SALARY']);
- $salary = $emp->appendChild($salary);
- }
- $dom->save("employees.xml");
- ?>
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:
- <?xml version="1.0" encoding="UTF-8"?>
- <EMPLOYEES>
- <EMPLOYEE id="100">
- <ENAME>King</ENAME>
- <SALARY>24000</SALARY>
- </EMPLOYEE>
- <EMPLOYEE id="101">
- <ENAME>Kochhar</ENAME>
- <SALARY>17000</SALARY>
- </EMPLOYEE>
- <EMPLOYEE id="102">
- <ENAME>De Haan</ENAME>
- <SALARY>17000</SALARY>
- </EMPLOYEE>
- </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.
Comments
Sponsored Links
