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(); trigger_error('Could not establish a connection: ' . $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); } echo $dom->saveXML(); $dom->save("employees.xml"); ?>
%3C%3Fphp%0D%0A%26nbsp%3B%20%26nbsp%3B%2F%2FFile%3A%20DOM.php%0D%0A%26nbsp%3B%20%26nbsp%3Bif%28%21%24rsConnection%20%3D%20oci_connect%28%27hr%27%2C%20%27hr%27%2C%20%27%2F%2Flocalhost%2Forcl%27%29%29%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24err%20%3D%20oci_error%28%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3Btrigger_error%28%27Could%20not%20establish%20a%20connection%3A%20%27%20.%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24err%5B%27message%27%5D%2C%20E_USER_ERROR%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%7D%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24dept_id%20%3D%2090%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24query%20%3D%20%22SELECT%20employee_id%2C%20last_name%2C%20salary%20FROM%20employees%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3BWHERE%20department_id%20%3D%20%3Adeptid%22%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24stmt%20%3D%20oci_parse%28%24rsConnection%2C%24query%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3Boci_bind_by_name%28%24stmt%2C%20%27%3Adeptid%27%2C%20%24dept_id%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bif%20%28%21oci_execute%28%24stmt%29%29%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3B%20%20%24err%20%3D%20oci_error%28%24stmt%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%20%20trigger_error%28%27Query%20failed%3A%20%27%20.%20%24err%5B%27message%27%5D%2C%20E_USER_ERROR%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%7D%0D%0A%26nbsp%3B%20%26nbsp%3B%24dom%20%3D%20new%20DOMDocument%28%271.0%27%2C%20%27UTF-8%27%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24root%20%3D%20%24dom-%3EcreateElement%28%27EMPLOYEES%27%2C%20%27%27%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24root%20%3D%20%24dom-%3EappendChild%28%24root%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bwhile%20%28%24row%20%3D%20oci_fetch_assoc%28%24stmt%29%29%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24emp%20%3D%20%24dom-%3EcreateElement%28%27EMPLOYEE%27%2C%20%27%27%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24emp%20%3D%20%24root-%3EappendChild%28%24emp%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24emp-%3EsetAttribute%28%27id%27%2C%20%24row%5B%27EMPLOYEE_ID%27%5D%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24ename%20%3D%20%24dom-%3EcreateElement%28%27ENAME%27%2C%20%24row%5B%27LAST_NAME%27%5D%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24ename%20%3D%20%24emp-%3EappendChild%28%24ename%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24salary%20%3D%20%24dom-%3EcreateElement%28%27SALARY%27%2C%20%24row%5B%27SALARY%27%5D%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24salary%20%3D%20%24emp-%3EappendChild%28%24salary%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%7D%0D%0A%26nbsp%3B%20%26nbsp%3Becho%20%24dom-%3EsaveXML%28%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24dom-%3Esave%28%22employees.xml%22%29%3B%0D%0A%3F%3E
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>
%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3CEMPLOYEES%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CEMPLOYEE%20id%3D%22100%22%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CENAME%3EKing%3C%2FENAME%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CSALARY%3E24000%3C%2FSALARY%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3C%2FEMPLOYEE%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3CEMPLOYEE%20id%3D%22101%22%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CENAME%3EKochhar%3C%2FENAME%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CSALARY%3E17000%3C%2FSALARY%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3C%2FEMPLOYEE%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3CEMPLOYEE%20id%3D%22102%22%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CENAME%3EDe%20Haan%3C%2FENAME%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3CSALARY%3E17000%3C%2FSALARY%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%3C%2FEMPLOYEE%3E%0D%0A%26nbsp%3B%20%26nbsp%3B%3C%2FEMPLOYEES%3E
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)
|