Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML and Oracle 8i : A How-To Guide for PL/SQL Users NoCOUG, 14 th Nov’00, 1.00 pm Eashwar Iyer.

Similar presentations


Presentation on theme: "XML and Oracle 8i : A How-To Guide for PL/SQL Users NoCOUG, 14 th Nov’00, 1.00 pm Eashwar Iyer."— Presentation transcript:

1 XML and Oracle 8i : A How-To Guide for PL/SQL Users NoCOUG, 14 th Nov’00, 1.00 pm Eashwar Iyer

2 Goals and Objectives Understand the basics of XML, DTD and XSL Look at some of the relevant tools offered by Oracle Understand the basic usage of these tools with the help of examples See the examples in action

3 Some Basics Revisited XML - eXtensible Markup Language It describes data in an easily readable format but without any indication of how the data is to be displayed (as in HTML). It is a database-neutral and device-neutral format; data marked up in XML can be targeted at different devices using eXtensible Style Language (XSL).

4 Some Basics Revisited Example of XML 05.00 hrs 00.00 hrs

5 Some Basics Revisited DTD – Document Type Definition Is a set of rules or grammar that is defined by us to construct our own XML rules. In other words, a DTD provides the rules that define the elements and structure of our new language.

6 Example of DTD Some Basics Revisited Note: The actual DTD syntax is covered in later slides

7 Some Basics Revisited XSL – eXtensible Style Language At its most basic, XSL provides a capability similar to a "mail merge." The style sheet contains a template of the desired result structure, and identifies data in the source document (XML) to insert into this template.

8 Example of XSL Some Basics Revisited HOUSE RULES Rule # Wake up at : Hit the bed at : Some Basics Revisited

9 Output of the example in a browser Some Basics Revisited

10 XML in Oracle Oracle8i with JServer (version used for the examples in this presentation is 8.1.6) Oracle XML Developer’s Kit (XDK) (among the many tools available in the XDK, the following are used for the examples here)  Oracle XML Parser for PL/SQL  Oracle XSU (XML SQL Utility)  XMLGEN Package

11 Read data from the database and convert them into an XML document. Output the XML documents in the appropriate device (we will restrict ourselves to displaying the output in a browser). Read XML document and insert the data contained in it into the table in the database. A Round-Trip Example What do we want to achieve? To enjoy all the benefits provided by the Oracle tools, the least we should be able to do, to get started, are:

12 A Round-Trip Example Lets consider a Zip code table with the following structure: Column Name Data Type Width State_Abbreviation Character 2 ZipCode Character 5 Zip_Code_Extn Character 4 City Varchar2 50

13 First Cut DTD

14 Extending the DTD Among the many keywords available for defining DTDs, let’s look at the “Attribute” keyword. <!ATTLIST zipcode zipcode CDATA #REQUIRED > <!ATTLIST city city CDATA #REQUIRED > <!ATTLIST state_abbreviation state (AL | AK | AZ | AR | AS | CA | CO | CT) #REQUIRED >

15 XSL for the XML/DTD Zipcode Zip Code Extn City State Abbreviation Contd.......

16 <xsl:value-of select="STATE_ABBREVIATION"/> XSL for the XML/DTD

17 D E M O N S T R A T I O N Implementing the example in Oracle

18 Code: To Create an XML Doc from a Table in the Database DATA XML DOCUMENT PL/SQL CODE DISPLAY IN BROWSER

19 declare xmlString CLOB := null; amount integer:= 1000; position integer := 1; charString varchar2(1000); fileHandle UTL_FILE.FILE_TYPE; begin --we want the result document root to be "Zipcodes" --to follow our DTD structure xmlgen.setRowsetTag('Zipcodes'); --we want the row element to be named "mappings" to follow our --DTD structure Contd....... Code: To Create an XML Doc from a Table in the Database

20 xmlgen.setRowTag('mappings'); --open the file in "write" mode fileHandle := utl_file.fopen('d:\test','XML_FOR_ZIPCODES.XML', 'w'); --set the ERROR tag to be ERROR_RESULTS xmlgen.setErrorTag('ERROR_RESULT'); --set the id attribute in the ROW element to be Record - so that it --shows the number of records fetched xmlgen.setRowIdAttrName('Record'); --do not use the null indicator to indicate nullness xmlgen.useNullAttributeIndicator(false); Contd....... Code: To Create an XML Doc from a Table in the Database

21 --attach the stylesheet to the result document xmlgen.setStyleSheet('XSL_FOR_ZIPCODES.XSL'); --this gets the XML out - the 0 indicates no DTD in the generated --XML document. A value of 1 will provide a DTD description in --the XML document xmlString := xmlgen.getXML('select * from scott.zipcodes',0); --now open the lob data. dbms_lob.open(xmlString, dbms_lob.lob_readonly); loop -- read the lob data dbms_lob.read(xmlString,amount,position,charString); Contd....... Code: To Create an XML Doc from a Table in the Database

22 utl_file.put_line(fileHandle, charString); position := position + amount; end loop; exception when no_data_found then -- end of fetch, free the lob dbms_lob.close(xmlString); dbms_lob.freetemporary(xmlString); xmlgen.resetOptions; utl_file.fclose(fileHandle); when others then xmlgen.resetOptions; end; Code: To Create an XML Doc from a Table in the Database

23 The XML Document in a Browser

24 create or replace procedure db2xml2browser is ------- xmlgen.setStyleSheet('\MYDIR\XSL_FOR_ZIPCODES.XSL'); ------- htp.prn(ltrim(charString,' ')); ------- Code: To Create an XML Doc “on the fly” from a Table in the Database

25 The XML Document in a Browser invoked via iAS

26 DATA XML DOCUMENT PL/SQL CODE Code: To Read Data from an XML Doc into a Table in the Database

27 declare charString varchar2(80); finalStr varchar2(4000) := null; rowsp integer; v_FileHandle UTL_FILE.FILE_TYPE; begin -- the name of the table as specified in our DTD xmlgen.setRowsetTag('Zipcodes'); -- the name of the data set as specified in our DTD xmlgen.setRowTag('mappings'); Contd.......

28 -- for getting the output on the screen dbms_output.enable(1000000); -- open the XML document in read only mode v_FileHandle := utl_file.fopen('d:\test','XML_NEW_CITIES.XML', 'r'); loop begin utl_file.get_line(v_FileHandle, charString); exception when no_data_found then utl_file.fclose(v_FileHandle); exit; end; C ontd....... Code: To Read Data from an XML Doc into a Table in the Database

29 dbms_output.put_line(charString); if finalStr is not null then finalStr := finalStr || charString; else finalStr := charString; end if; end loop; -- for inserting the XML data into the table rowsp := xmlgen.insertXML('scott.zipcodes',finalStr); dbms_output.put_line('Insert Done '||to_char(rowsp)); xmlgen.resetOptions; end; Code: To Read Data from an XML Doc into a Table in the Database

30 The Sample XML Doc CA 94301 Palo Alto CO 80323 9277 Boulder

31 New Records in the Table SQL> select * from zipcodes; ST ZIPCO ZIP_ CITY -- ----- ---- ------- ------------- CA 95123 6111 San Jose CA 95234 Sunnyvale AK 72701 Fayetteville CA 94301 Palo Alto CO 80323 9277 Boulder

32 Conclusion We saw the definition of XML, DTD and XSL. We acquainted ourselves with the names of some of the Oracle Tools available for handling XML. We saw these tools in action with some examples. The topics covered in this session are building blocks for the complex, real life requirements of enterprises. The available Oracle tools are extremely powerful and geared to handle all these complexities.

33 A Q & Q U E S T I O N S A N S W E R S

34 About Quovera, Redwood Shores, CA, is a Full Service Provider (FSP) that delivers e-business solutions in e-time designed for dot-com, Fortune 1000 and high-growth companies. With over five years proven experience, Quovera provides a single source for Business Modeling, Integrated e-Services, ASP and Strategic Business Process Outsourcing. http://www.quovera.com For the Presentation and White Paper download, visit http://www.quovera.com/forum/index.html NoCOUG 2000, 14 th Nov’00, 1.00pm


Download ppt "XML and Oracle 8i : A How-To Guide for PL/SQL Users NoCOUG, 14 th Nov’00, 1.00 pm Eashwar Iyer."

Similar presentations


Ads by Google