Using PHP 5 with Oracle XML DBDEVELOPER: PHPBy Yuli Vasiliev. Combine the power of object- oriented PHP and Oracle XML DB. As a programming language, PHP has many strengths, including ease of use, short development time, and high performance. With a brand- new object model, PHP 5 leverages the power and flexibility of object- oriented programming. This fifth release of PHP brings many more fully object- oriented capabilities to the language, including interfaces; abstract classes; private, public, and protected access modifiers; and static members and methods. PHP 5 also now passes objects by reference rather than by value. PHP provides several built- in class libraries you can easily extend, thus putting the promise of reuse into immediate practice. Your free warez center! Are you new to Oracle Community? Are you an experienced member who wants to help out our new users? Check out our new Getting Started group, and join today! How to install files downloaded from torrent? Please upload a file. This article covers extending one of the classes from PHP's Document Object Model (DOM) library, creating a subclass that interacts with Oracle XML DB. It examines some of the object- oriented features of the PHP classes and goes through some changes to the sample subclass that exercise various ways of working with Oracle XML DB from PHP. Demonstrating Inheritance Features of PHP 5 Using Oracle XML DBIn PHP 5, a class inherits the functionality of an existing base class by using the extends keyword. Just as in Java and other object- oriented programming languages, you can extend both user- defined and predefined PHP classes in PHP 5. The DOM function library is one of the predefined PHP classes available automatically in PHP 5—it provides functions for creating, populating, and otherwise working with XML DOM structures (document nodes, elements, and so on); it is an object- oriented class library that follows DOM Level 2 as much as possible (the PHP 5 DOM library replaces PHP 4's DOM XML extension). Extending a Built- in Class. In Listing 1, the My. Dom. Document class code in My. Dom. Document. php illustrates how you can extend the predefined Dom. Document class with a public method that is intended to simplify the process of creating XML documents. Parent Server Won't Update Itself - Def Download Failed. One thing I noticed is that the new.xdb file that is downloaded. Code Listing 1: My. Dom. Document class extends Dom. Document. //File: My. Dom. Document. php. My. Dom. Document extends Dom. Document! Unlike other object- oriented languages, PHP doesn't implicitly invoke the parent constructor when the subclass calls its constructor, which means that if you define a constructor in a child class, you must explicitly call the constructor of the parent class from within the new child constructor. Also note that, as with Java, only members and methods defined in the parent as public or protected are accessible within a child class. Regardless of whether a child class overrides a public or protected parent method or member, you can still access it within that child class, by using the parent: : prefix. To client code, any method or member defined as public in the parent class will be accessible with an instance of the child class, by default, unless this child class has overridden that parent method or member. The dom. php script shown in Listing 2 creates an instance of My. Dom. Document and populates an XML DOM structure. If you load dom. php in the /htdocs (or other appropriate directory) of your Web server and open the file in a Web browser, you'll see the following output when you view the source. Scott. Cred. php contains the logon credentials and connection string information for the Oracle database, and db. Conn. 5. php comprises a PHP 5- compliant db. Conn. 5 connection string class. You'll want to modify the particulars ( $user, $pswd , and connection string information) of the Scott. Cred. php file for your specific environment if you run the sample code for this article, but you can leave db. Conn. 5 . php as is. Assuming you're connected to an Oracle database that has the default SCOTT database schema, with its EMP table, dom. XML. php or dom. Tagged. XML. php would generate this XML output. Let's go a step further and save the XML content (stored in the internal XML tree of the My. Dom. Document class object) to an Oracle database, using Oracle XML DB. Using the Database to Save Your XMLProceeding with the example, you can go one step further and save the XML content stored in the internal XML tree of the My. Dom. Document instance to an Oracle database. You might, for example, want to extract the employee XML elements from the XML tree and save them as individual separate employee XML documents in the database. To do this, you must first create a database schema in which to store the employee XML documents, defining an XMLType for storage of employee XML documents using an XML Schema definition. In this example, the insert. Schema. Based. XML() method takes an instance of db. Conn. 5 as a parameter and then invokes the query() method to perform the INSERT operation against the database. With the insert. Schema. Based. XML() method in place, you don't need to compose individual INSERT statements to insert schema- based XML data into an Oracle database: insert. Schema. Based. XML() will do it for you. As shown by the insert. Schema. Based. XML() method signature in Listing 7, you must specify the following parameters: A tag name ( $tagname ) to search for appropriate elements within the internal XML tree of the My. Dom. Document instance. Each of these elements will be used as an independent XML document. An instance of the db. Conn. 5 class to interact with the database. The XMLType table ( $table ) into which to insert the XML content. A registered XML schema to validate the inserted XML documents. As a result, the work of inserting the generated employee XML documents into the database is reduced to the following. Schema. Based. XML(. EMPLOYEE', $db. 2,'employee'. The dom. XML. If you try to run the dom. XML. Although this may seem odd—a class definition typically doesn't include try- catch routines—it makes sense in this example, because the interaction is between two classes: The code in the try block calls the query() method in the db. Conn. 5 class, which can throw an exception. The query() method throws an exception if it can't perform the query against the database, so if you omit the try- catch statement in the insert. Schema. Based. XML() method, an exception thrown in the query() method will be caught only in the calling script. This means that the insert. Schema. Based. XML() method will not terminate when query() fails to insert a row (an XML document) into the specified XMLType table. Instead, it will try to insert the following row until it reaches the end of the list of the generated XML documents. To prevent this behavior, you simply wrap the code calling the query() method in the try block and define a catch block that, in response to an exception, will throw a new exception, thereby terminating insert. Schema. Based. XML() execution when the first failure to insert a row occurs. However, if you want the insert. Schema. Based. XML() method to continue execution when an insert failure has occurred, thereby trying to insert the following row, you can simply replace the line of code that throws an exception in the catch block with a line that just prints a warning message. In this case, the catch block in the insert. Schema. Based. XML() method would look like this. Exception $e) . In fact, Oracle XML DB provides you with different strategies for moving XML content into and out of the Oracle database. For example, you can use industry- standard internet protocols, such as FTP, HTTP, or Web. DAV, to insert XML content into the database. In this case, you simply put the files that contain the schema- based XML content into a folder in the Oracle XML DB repository. Oracle XML DB then implicitly inserts the appropriate rows into the table specified as the default table for that XML schema. For example, to add a function to insert XML via FTP programmatically, you could add an insert. XMLBy. FTP() public method to the My. Dom. Document class (of Listing 1), as shown in Listing 9. Code Listing 9: insert. XMLBy. FTP—internet protocol alternative to SQL public function insert. XMLBy. FTP($tag. Name, $table, $user,$pswd, $ftp. Oracle Database, in turn, automatically inserts the appropriate row into the employee table of XMLType in the PHPUSR database schema. You can confirm the success of the INSERT operations by logging on to the database as PHPUSR from SQL*Plus and obtaining the row count as follows. SELECT COUNT(*) FROM employee. Although executing dom. XMLFTP. Specifically, whereas FTP and other internet protocols don't let you explicitly control transactions from your PHP code, the SQL- based approach does. However, you can easily change the default behavior to explicitly control the transaction from your PHP script, by specifying the OCI!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |