Top Banner
Overview The following article shows some of the possibilities how XML documents can be created with PL/SQL and Oracle 8i or 9i. The discussed topics are: XMLGEN Package XML-SQL Utility for PL/SQL XMLDOM Package The examples use the well known tables DEPT (the master table) and EMP (the detail table): Every employee has exactly one department assigned. One department has no, one, or more employees. SELECT deptno, dname FROM dept; DEPTNO DNAME ---------- -------------- 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS SELECT empno, ename, deptno FROM emp; EMPNO ENAME DEPTNO ---------- ---------- ---------- 7369 SMITH 20 7499 ALLEN 30 7521 WARD 30 7566 JONES 20 7654 MARTIN 30 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: XML Creation Using Plsql

Overview

The following article shows some of the possibilities how XML documents can be created with PL/SQL and Oracle 8i or 9i. The discussed topics are:

XMLGEN Package XML-SQL Utility for PL/SQL

XMLDOM Package

The examples use the well known tables DEPT (the master table) and EMP (the detail table): Every employee has exactly one department assigned. One department has no, one, or more employees.

SELECT deptno, dname FROM dept;

    DEPTNO DNAME---------- --------------        10 ACCOUNTING        20 RESEARCH        30 SALES        40 OPERATIONS

SELECT empno, ename, deptno FROM emp;

      EMPNO ENAME          DEPTNO---------- ---------- ----------      7369 SMITH              20      7499 ALLEN              30      7521 WARD               30      7566 JONES              20      7654 MARTIN             30      7698 BLAKE              30      7782 CLARK              10      7788 SCOTT              20      7839 KING               10      7844 TURNER             30      7876 ADAMS              20      7900 JAMES              30      7902 FORD               20      7934 MILLER             10

XMLGEN Package

Page 2: XML Creation Using Plsql

The simplest way to generate an XML document is the usage of the package XMLGEN. The function getXml() takes the parameters for the SQL query and the meta data type (such as DTD) and returns a CLOB containing the XML document.

SELECT xmlgen.getXml(  'SELECT empno "EMP_NO"        , ename "NAME"        , deptno "DEPT_NO"     FROM emp    WHERE deptno = 10'  , 0  ) FROM dual;

<?xml version = '1.0'?><ROWSET>   <ROW num="1">      <EMP_NO>7782</EMP_NO>      <NAME>CLARK</NAME>      <DEPT_NO>10</DEPT_NO>   </ROW>   <ROW num="2">      <EMP_NO>7839</EMP_NO>      <NAME>KING</NAME>      <DEPT_NO>10</DEPT_NO>   </ROW>   <ROW num="3">      <EMP_NO>7934</EMP_NO>      <NAME>MILLER</NAME>      <DEPT_NO>10</DEPT_NO>   </ROW></ROWSET>

The result is a root element named ROWSET, which contains a list of ROW-elements. Each ROW-element has the row number as attribute and each ROW-element contains the elements EMP_NO, NAME, and DEPT_NO. Note that the element names of the ROW-elements are slightly different from the column names of the EMP table!

Using XML-SQL Utility (XSU)

XML-SQL Utility (XSU) provides a simple way of achieving data transformation by mapping any SQL query to XML and vice versa. XSU provides the basic functionality to get and put data to and from a database.

The DBMS_XMLQUERY and DBMS_XMLSAVE are two packages that support XML generation and storage through XML. Here we will focus on XML generation.

Page 3: XML Creation Using Plsql

Generating XML by invoking getXML() function results in a CLOB that contains the XML document. A context handle needed in most subsequent calls is created in the first step.

DECLARE  queryCtx dbms_xmlquery.ctxType;  result CLOB;BEGIN  -- set up the query context  queryCtx := dbms_xmlquery.newContext(    'SELECT empno "EMP_NO"          , ename "NAME"          , deptno "DEPT_NO"       FROM emp      WHERE deptno = :DEPTNO'  );

If the DTD or XML schema definition has explicitly defined tag names others than the column names then you can change the ROW and ROWSET tag names easily:

  dbms_xmlquery.setRowTag(      queryCtx    , 'EMP'  );  dbms_xmlquery.setRowSetTag(      queryCtx    , 'EMPSET'  );

Before executing the query you must bind the values to the SQL statement. The named bind variables have to start with a ’:’ in front.

  dbms_xmlquery.setBindValue(      queryCtx     , 'DEPTNO'     , 10  );

Ok, now you are ready to run the query and generate the XML result as CLOB. A simple procedure printClobOut() supports printing out a CLOB to screen. Finally the query handle must be closed to free the resources.

  result := dbms_xmlquery.getXml(queryCtx);  printClobOut(result);  dbms_xmlquery.closeContext(queryCtx);END;/

Page 4: XML Creation Using Plsql

The result is something like

<?xml version = '1.0'?><EMPSET>   <EMP num="1">      <EMP_NO>7782</EMP_NO>      <NAME>CLARK</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP>   <EMP num="2">      <EMP_NO>7839</EMP_NO>      <NAME>KING</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP>   <EMP num="3">      <EMP_NO>7934</EMP_NO>      <NAME>MILLER</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP></EMPSET>

Moreover you can use further functionality provided by the DBMS_XMLQUERY such as

Paginating by using setMaxRows() and setSkipRows(). Setting and applying stylesheets by using setStylesheetHeader() and useStylesheet(). The

utility generates the XML document, calls the parser, applies the stylesheet, and generates the result which is a huge performance win.

The package DBMS_XMLQUERY supports XML generation. In a similar way you can use DBMS_XMLSAVE to store XML data directly in the database. Possible operations are insertXML(), updateXML(), and deleteXML().

XMLDOM Package

The XMLDOM package implements the Document Object Model Interface (DOM Interface) as defined by W3C XML recommendations. Some of the interfaces are:

DOM Attribute interface DOM Document interface

DOM DocumentType interface

DOM DOMImplementation interface

DOM Element interface

Page 5: XML Creation Using Plsql

DOM Node interface

DOM NodeList interface

DOM Notation interface

DOM ProcessingInstruction interface

DOM Text interface

Let’s go through a simple example to discuss the usage of the XMLDOM package. Give special privileges to the user running this code. A private synonym simplifies the access to the package. For example, for user scott:

GRANT javauserpriv to scott;GRANT javasyspriv to scott;GRANT EXECUTE ON xmldom TO scott;CREATE SYNONYM scott.xmldom FOR SYS.xmldom;

In the declaration section, you need a set of DOM references. The DOMDocument handle is the most important. It will be used in most subsequent calls. Moreover you need different DOM node handles to reference the main node, the root node, the user node, and the item node for each element. The cursor selects the employees for a given department.

DECLARE  doc xmldom.DOMDocument;  main_node xmldom.DOMNode;  root_node xmldom.DOMNode;  user_node xmldom.DOMNode;  item_node xmldom.DOMNode;  root_elmt xmldom.DOMElement;  item_elmt xmldom.DOMElement;  item_text xmldom.DOMText;  CURSOR get_users(p_deptno NUMBER) IS    SELECT empno         , ename         , deptno         , rownum      FROM emp     WHERE deptno = p_deptno;

First of all you create a new document handle. Next you create the main node for this document. The root element is named EMPSET and appended as child node to the main node. The returned handle is used as root node for subsequent calls.

Page 6: XML Creation Using Plsql

BEGIN  doc := xmldom.newDOMDocument;  main_node := xmldom.makeNode(doc);  root_elmt := xmldom.createElement(      doc    , 'EMPSET'  );  root_node := xmldom.appendChild(      main_node    , xmldom.makeNode(root_elmt)  );

For every record found in the query a new element named EMP is created. Analogue to the previous samples, the row number is added as attribute to the element. This element is appended as child node to the root node. The returned handle is used as user node for subsequent calls.

  FOR get_users_rec IN get_users(10) LOOP    item_elmt := xmldom.createElement(        doc      , 'EMP'    );    xmldom.setAttribute(        item_elmt      , 'num'      , get_users_rec.rownum    );    user_node := xmldom.appendChild(        root_node      , xmldom.makeNode(item_elmt)    );

Now the text elements can be added to the DOM document. In the first step a new element named EMP_NO is created. This element is appended as child node to the user node. In the second step  a text node is created which contains the record data, in this case the employee number. This text node is appended as child node to the item node.

    item_elmt := xmldom.createElement(        doc      , 'EMP_NO'    );    item_node := xmldom.appendChild(        user_node      , xmldom.makeNode(item_elmt)    );    item_text := xmldom.createTextNode(        doc

Page 7: XML Creation Using Plsql

      , get_users_rec.empno    );    item_node := xmldom.appendChild(        item_node      , xmldom.makeNode(item_text)    );

The same can be done with the text elements NAME and DEPT_NO.

After all records have been processed and all data has been loaded into the DOM document it can be e.g. filed out and its resources can be released:

  END LOOP;  xmldom.writeToFile(      doc    , '/tmp/xml/docSample.xml'  );  xmldom.freeDocument(doc);END;/

Note that the XMLDOM package is able to write the XML file in every location accessible by OS user oracle, regardless of the current initialisation parameter UTL_FILE_DIR. The resulting file contains the following lines:

<?xml version = '1.0' encoding = 'UTF-8'?><EMPSET>   <EMP num="1">      <EMP_NO>7782</EMP_NO>      <NAME>CLARK</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP>   <EMP num="2">      <EMP_NO>7839</EMP_NO>      <NAME>KING</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP>   <EMP num="3">      <EMP_NO>7934</EMP_NO>      <NAME>MILLER</NAME>      <DEPT_NO>10</DEPT_NO>   </EMP></EMPSET>

Even if the usage of XMLDOM package seems to be complex at the beginning, the structure of the DOM document is build up in a simple way: Every element or attribute must be added as a

Page 8: XML Creation Using Plsql

node to a parent node. The same approach with equivalent methods is used within Java and its XML implementation.

For easier handling of the common functionality you may write your own XML library to simplify for example adding text nodes to parent nodes.

Some limitations: Keep in mind, that the whole DOM tree is kept in memory until the document is freed. Depending on database memory configuration the total number of nodes is limited, e.g. with JAVA_POOL_SIZE of  10’0000'000 bytes we could add up to 20'000 nodes.

Another limitation is a memory leak in the Java implementation of the DOM interface and XML parser. This bug is known under Sun Solaris and can be fixed with Oracle 8.1.7.3 and bug fix 2104071 or with Oracle 9.2.

Conclusion

The XMLGEN package can be used for simple XML document generation. The data can be fetched from a single SQL statement.

XSU with the two packages DBMS_XMLQUERY and DBMS_XMLSAVE supports XML generation and storage through XML. Additional functionality allows the developer to bind variables, to paginate the result set, or to apply stylesheets directly.

The most sophisticated approach is the XMLDOM package. Here you have the full flexibility provided by the DOM interface. Every given DTD or XML schema definition can be implemented using complex elements, attributes and more.

Links and Documents

Further articles about generating XML documents with PL/SQL

Oracle XML Reference Guide

Oracle Application Developer's Guide - XML

Download Scripts

Create XML docs using XSU (XML-SQL Utility) Create XML docs using XMLGEN package

Create XML docs using XMLDOM package

Page 9: XML Creation Using Plsql

---------------------------------------------------------------------------------- Akadia Create XML docs---- (c) Akadia AG 2002---- Requires Oracle 8i or 9i---- Create XML docs using XSU (XML-SQL Utility).--------------------------------------------------------------------------------CONNECT scott/tiger@sid--SET SERVEROUTPUT ON---- Simple procedure to support printing out a CLOB to screenCREATE OR REPLACE PROCEDURE printClobOut(result IN OUT NOCOPY CLOB) IS xmlstr VARCHAR2(32767); line VARCHAR2(2000);BEGIN xmlstr := dbms_lob.substr(result,32767); LOOP EXIT WHEN xmlstr IS NULL; line := substr(xmlstr,1,instr(xmlstr,chr(10))-1); dbms_output.put_line(line); xmlstr := substr(xmlstr,instr(xmlstr,chr(10))+1); END LOOP;END;/SHOW ERRORS;---- Simple example with binding variableDECLARE queryCtx dbms_xmlquery.ctxType; result CLOB;BEGIN -- set up the query context queryCtx := dbms_xmlquery.newContext( 'SELECT empno "EMP_NO" , ename "NAME" , deptno "DEPT_NO" FROM emp WHERE deptno = :DEPTNO'

Page 10: XML Creation Using Plsql

); -- set row tag name and row set tag name dbms_xmlquery.setRowTag( queryCtx , 'EMP' ); dbms_xmlquery.setRowSetTag( queryCtx , 'EMPSET' ); -- bind variables dbms_xmlquery.setBindValue( queryCtx , 'DEPTNO' , 10 ); -- run query and print out result result := dbms_xmlquery.getXml(queryCtx); printClobOut(result); -- free resources dbms_xmlquery.closeContext(queryCtx);END;/

CONNECT scott/tiger@sid--SELECT xmlgen.getXml( 'SELECT empno "EMP_NO" , ename "NAME" , deptno "DEPT_NO" FROM emp WHERE deptno = 10' , 0 ) FROM dual

Page 11: XML Creation Using Plsql

CONNECT sys/manager@sid--GRANT javauserpriv to scott;GRANT javasyspriv to scott;GRANT EXECUTE ON xmldom TO scott;--CREATE SYNONYM scott.xmldom FOR SYS.xmldom;--CONNECT scott/tiger@sid--DECLARE doc xmldom.DOMDocument; main_node xmldom.DOMNode; root_node xmldom.DOMNode; user_node xmldom.DOMNode; item_node xmldom.DOMNode; root_elmt xmldom.DOMElement; item_elmt xmldom.DOMElement; item_text xmldom.DOMText; CURSOR get_users(p_deptno NUMBER) IS SELECT empno , ename , deptno , rownum FROM emp WHERE deptno = p_deptno;BEGIN -- get document doc := xmldom.newDOMDocument; -- create root element main_node := xmldom.makeNode(doc); root_elmt := xmldom.createElement( doc , 'EMPSET' ); -- xmldom.setAttribute(root_elmt, 'xmlns', 'http://www.akadia.com/xml/soug/xmldom'); -- xmldom.setAttribute(root_elmt, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); -- xmldom.setAttribute(root_elmt, 'xsi:schemaLocation', 'http://www.akadia.com/xml/soug/xmldom/emp.xsd'); root_node := xmldom.appendChild( main_node , xmldom.makeNode(root_elmt) );

Page 12: XML Creation Using Plsql

FOR get_users_rec IN get_users(10) LOOP -- create user element with rownum as attribute item_elmt := xmldom.createElement( doc , 'EMP' ); xmldom.setAttribute( item_elmt , 'num' , get_users_rec.rownum ); user_node := xmldom.appendChild( root_node , xmldom.makeNode(item_elmt) ); -- create user element: EMP_NO item_elmt := xmldom.createElement( doc , 'EMP_NO' ); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) ); item_text := xmldom.createTextNode( doc , get_users_rec.empno ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); -- create user element: NAME item_elmt := xmldom.createElement( doc , 'NAME' ); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) ); item_text := xmldom.createTextNode( doc , get_users_rec.ename ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); -- create user element: DEPT_NO item_elmt := xmldom.createElement( doc , 'DEPT_NO'); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) );

Page 13: XML Creation Using Plsql

item_text := xmldom.createTextNode( doc , get_users_rec.deptno ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); END LOOP; -- write document to file using default character set from database xmldom.writeToFile( doc , '/tmp/xml/docSample.xml' ); -- free resources xmldom.freeDocument(doc);END;/

-- deal with exceptionsEXCEPTION

WHEN xmldom.INDEX_SIZE_ERR THEN raise_application_error(-20120, 'Index Size error');WHEN xmldom.DOMSTRING_SIZE_ERR THEN raise_application_error(-20120, 'String Size error');WHEN xmldom.HIERARCHY_REQUEST_ERR THEN raise_application_error(-20120, 'Hierarchy request error');WHEN xmldom.WRONG_DOCUMENT_ERR THEN raise_application_error(-20120, 'Wrong doc error');WHEN xmldom.INVALID_CHARACTER_ERR THEN raise_application_error(-20120, 'Invalid Char error');WHEN xmldom.NO_DATA_ALLOWED_ERR THEN raise_application_error(-20120, 'Nod data allowed error');WHEN xmldom.NO_MODIFICATION_ALLOWED_ERR THEN raise_application_error(-20120, 'No mod allowed error');WHEN xmldom.NOT_FOUND_ERR THEN raise_application_error(-20120, 'Not found error');WHEN xmldom.NOT_SUPPORTED_ERR THEN raise_application_error(-20120, 'Not supported error');WHEN xmldom.INUSE_ATTRIBUTE_ERR THEN raise_application_error(-20120, 'In use attr error');END;

XML File Creation Thru PL/SQL

CREATE OR REPLACEPROCEDURE gl_inter_company_trans(retcode OUT VARCHAR2,errbuf OUT VARCHAR2,p_period VARCHAR2,p_operating_unit VARCHAR2,

Page 14: XML Creation Using Plsql

p_status1 VARCHAR2)ISCURSOR gl_detail(p_status IN VARCHAR2,p_period VARCHAR2,p_operating_unit VARCHAR2)ISSELECTreceiver,sender,transaction_number,period,entered_date,description,note,amount,NAME,attribute10,status,gl_date,sendor_gl_transfer,receiver_gl_transfer,CONTEXTFROM(SELECTrsub.NAME receiver,ssub.NAME sender,gl_.transaction_number,gl_.sender_period_name period,gl_.entered_date,gl_.description,gl_.note,( NVL (gl_.sender_running_total_dr, 0) - NVL (gl_.sender_running_total_cr, 0) ) amount,REPLACE (typ.NAME, '&', '') NAME,gl_.attribute10,DECODE (gl_.status, 'R', 'Review', 'Approved') status,gl_.gl_date,DECODE (gl_.sender_transfer_flag, 'Y', 'Yes', 'No')sendor_gl_transfer,DECODE (gl_.receiver_transfer_flag, 'Y', 'Yes', 'No')receiver_gl_transfer,gl_.CONTEXTFROMgl.gl_iea_transactions gl_,

Page 15: XML Creation Using Plsql

gl.gl_iea_transaction_types typ,gl.gl_iea_subsidiaries ssub,gl.gl_iea_subsidiaries rsubWHEREtyp.transaction_type_id = gl_.transaction_type_idAND ssub.subsidiary_id = gl_.sending_subsidiary_idAND rsub.subsidiary_id = gl_.receiving_subsidiary_idAND gl_.status = 'R'AND gl_.status = NVL (p_status, gl_.status)AND gl_.sender_period_name = NVL (p_period, gl_.sender_period_name)AND(ssub.NAME = NVL (p_operating_unit, ssub.NAME)OR rsub.NAME = NVL (p_operating_unit, rsub.NAME)));p_status VARCHAR2 (100);v_transaction_num_prev VARCHAR2 (50) := '00000';v_transaction_num_curr VARCHAR2 (50) := '11111';BEGINBEGINIF p_status1 = 'Approved'THENp_status := 'A';ELSIF p_status1 = 'Review'THENp_status := 'R';ELSIF p_status1 = 'ALL'THENp_status := NULL;END IF;END;fnd_file.put_line (fnd_file.output, '(?xml version="1.0" encoding="UTF-8"?)');fnd_file.put_line (fnd_file.output, '(Pending_Transac)'); -- Main Tagfnd_file.put_line (fnd_file.output, '(PERIOD)' || p_period || '');fnd_file.put_line (fnd_file.output, '(OPERATING_UNIT)' || p_operating_unit ||'(/OPERATING_UNIT)');fnd_file.put_line (fnd_file.output, '(STATUS)' || p_status1 || '(/STATUS)');FOR rec_gl_detail IN gl_detail (p_status, p_period, p_operating_unit)LOOPv_transaction_num_curr := rec_gl_detail.transaction_number;IF p_operating_unit IS NOT NULLTHENfnd_file.put_line (fnd_file.output, '(G_GL_DETAIL)'); -- Masters tagfnd_file.put_line (fnd_file.output, '(STATUS)' || rec_gl_detail.status ||

Page 16: XML Creation Using Plsql

'(/STATUS)');fnd_file.put_line (fnd_file.output, '(TRANSACTION_NUMBER)' ||rec_gl_detail.transaction_number '(/TRANSACTION_NUMBER)');fnd_file.put_line (fnd_file.output, '(ENTERED_DATE)' ||rec_gl_detail.entered_date || '(/ENTERED_DATE)');fnd_file.put_line (fnd_file.output, '(GL_DATE)' || rec_gl_detail.gl_date|| '(/GL_DATE)');fnd_file.put_line (fnd_file.output, '(PERIOD)' || rec_gl_detail.period ||'(/PERIOD)');fnd_file.put_line (fnd_file.output, '(NAME)' || rec_gl_detail.NAME ||'(/NAME)');fnd_file.put_line (fnd_file.output, '(SENDER)' || rec_gl_detail.sender ||'(/SENDER)');fnd_file.put_line (fnd_file.output, '(SENDOR_GL_TRANSFER)' ||rec_gl_detail.sendor_gl_transfer || '(/SENDOR_GL_TRANSFER)');fnd_file.put_line (fnd_file.output, '(RECEIVER)' ||rec_gl_detail.receiver || '(/RECEIVER)');fnd_file.put_line (fnd_file.output, '(RECEIVER_GL_TRANSFER)' ||rec_gl_detail.receiver_gl_transfer || '(/RECEIVER_GL_TRANSFER)');fnd_file.put_line (fnd_file.output, '(AMOUNT)' || rec_gl_detail.amount ||'(/AMOUNT)');fnd_file.put_line (fnd_file.output, '(DESCRIPTION)' ||rec_gl_detail.description || '(/DESCRIPTION)');fnd_file.put_line (fnd_file.output, '(NOTE)' || rec_gl_detail.note ||'(/NOTE)');fnd_file.put_line (fnd_file.output, '(ATTRIBUTE10)' ||rec_gl_detail.attribute10 || '(/ATTRIBUTE10)');fnd_file.put_line (fnd_file.output, '(CONTEXT)' || rec_gl_detail.CONTEXT|| '(/CONTEXT)');fnd_file.put_line (fnd_file.output, '(/G_GL_DETAIL)');ELSIF p_operating_unit IS NULLTHENIF ( ( v_transaction_num_curr <> v_transaction_num_prevAND rec_gl_detail.status = 'Approved')OR rec_gl_detail.status = 'Review')THENfnd_file.put_line (fnd_file.output, '(G_GL_DETAIL)'); -- Masters tagfnd_file.put_line (fnd_file.output, '(STATUS)' || rec_gl_detail.status|| '(/STATUS)');fnd_file.put_line (fnd_file.output, '(TRANSACTION_NUMBER)' ||rec_gl_detail.transaction_number || '(/TRANSACTION_NUMBER)');fnd_file.put_line (fnd_file.output, '(ENTERED_DATE)' ||rec_gl_detail.entered_date || '(/ENTERED_DATE)');fnd_file.put_line (fnd_file.output, '(GL_DATE)' ||rec_gl_detail.gl_date || '(/GL_DATE)');fnd_file.put_line (fnd_file.output, '(PERIOD)' || rec_gl_detail.period

Page 17: XML Creation Using Plsql

|| '(/PERIOD)');fnd_file.put_line (fnd_file.output, '(NAME)' || rec_gl_detail.NAME ||'(/NAME)');fnd_file.put_line (fnd_file.output, '(SENDER)' || rec_gl_detail.sender|| '(/SENDER)');fnd_file.put_line (fnd_file.output, '(SENDOR_GL_TRANSFER)' ||rec_gl_detail.sendor_gl_transfer || '(/SENDOR_GL_TRANSFER)');fnd_file.put_line (fnd_file.output, '(RECEIVER)' ||rec_gl_detail.receiver || '(/RECEIVER)');fnd_file.put_line (fnd_file.output, '(RECEIVER_GL_TRANSFER)' ||rec_gl_detail.receiver_gl_transfer || '(/RECEIVER_GL_TRANSFER)');fnd_file.put_line (fnd_file.output, '(AMOUNT)' || rec_gl_detail.amount|| '(/AMOUNT)');fnd_file.put_line (fnd_file.output, '(DESCRIPTION)' ||rec_gl_detail.description || '(/DESCRIPTION)');fnd_file.put_line (fnd_file.output, '(NOTE)' || rec_gl_detail.note ||'(/NOTE)');fnd_file.put_line (fnd_file.output, '(ATTRIBUTE10)' ||rec_gl_detail.attribute10 || '(/ATTRIBUTE10)');fnd_file.put_line (fnd_file.output, '(CONTEXT)' ||rec_gl_detail.CONTEXT || '(/CONTEXT)');fnd_file.put_line (fnd_file.output, '(/G_GL_DETAIL)');END IF;v_transaction_num_prev := v_transaction_num_curr;END IF;END LOOP;fnd_file.put_line (fnd_file.output, '(/Pending_Transac)'); -- End Main TagEXCEPTIONWHEN OTHERSTHENfnd_file.put_line (fnd_file.LOG, 'Entered into Exception');END gl_inter_company_trans;

create or replace procedure SP_XML_TEST isbegindeclare xmlString CLOB := null; -- Here we are reading 250 bytes at a time. We should be really reading a -- whole chunk. dbms_output.put_line can only accomodate 256 characters per line -- so we have this limitation. amount integer:= 255; position integer := 1; charString varchar2(255);

begin

xmlgen.setRowTag('EMP_ROW');                     -- we want the row element to be    named    EMP_ROW.

Page 18: XML Creation Using Plsql

 xmlgen.setRowsetTag('EMP_RESULTS');        -- we want the result document root to be EMP_RESULTS.  xmlgen.setMaxRows(3);                                       -- limit the output to 3 rows.  xmlgen.setskipRows(2);                                       -- skip the first two rows in the query before outputing results.  xmlgen.useLowerCaseTagNames();                   -- set the tag names to be all in lower case.  xmlgen.setErrorTag('ERROR_RESULT');           -- set the ERROR tag to be ERROR_RESULTS.  xmlgen.setRowIdAttrName('ENO');                    -- set the id attribute in the ROW element to be ENO.  xmlgen.setRowIdColumn('EMPNO');                 -- use the EMPNO column's value for the id attribute.  xmlgen.useNullAttributeIndicator(false);          -- do not use the null indicator to indicate nullness.  xmlgen.setStyleSheet('http://www.oracle.com/xsl');  -- attach the stylesheet PI to the result document.

 xmlString := xmlgen.getXML('select * from scott.emp ',1); -- This gets the XML out

 dbms_lob.open(xmlString,DBMS_LOB.LOB_READONLY);  -- Now open the lob data..  loop  dbms_lob.read(xmlString,amount,position,charString);      -- read the lob data  dbms_output.put_line(charString);  position := position + amount;  end loop;

 exception  when no_data_found then dbms_lob.close(xmlString);                                                   -- end of    fetch, free the lob dbms_lob.freetemporary(xmlString); xmlgen.resetOptions; when others then  xmlgen.resetOptions;   end;

   end SP_XML_TEST;

------------------------------------------------------------------------------- Akadia Create XML docs---- (c) Akadia AG 2002---- Requires Oracle 8i or 9i-- For larger amounts of nodes use 8.1.7.3 with bug fix 2104071 or 9.2---- Create XML docs using XMLDOM package.------------------------------------------------------------------------------CONNECT sys/manager@sid

Page 19: XML Creation Using Plsql

--GRANT javauserpriv to scott;GRANT javasyspriv to scott;GRANT EXECUTE ON xmldom TO scott;--CREATE SYNONYM scott.xmldom FOR SYS.xmldom;--CONNECT scott/tiger@sid--DECLARE doc xmldom.DOMDocument; main_node xmldom.DOMNode; root_node xmldom.DOMNode; user_node xmldom.DOMNode; item_node xmldom.DOMNode; root_elmt xmldom.DOMElement; item_elmt xmldom.DOMElement; item_text xmldom.DOMText; CURSOR get_users(p_deptno NUMBER) IS SELECT empno , ename , deptno , rownum FROM emp WHERE deptno = p_deptno;BEGIN -- get document doc := xmldom.newDOMDocument; -- create root element main_node := xmldom.makeNode(doc); root_elmt := xmldom.createElement( doc , 'EMPSET' ); -- xmldom.setAttribute(root_elmt, 'xmlns', 'http://www.akadia.com/xml/soug/xmldom'); -- xmldom.setAttribute(root_elmt, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); -- xmldom.setAttribute(root_elmt, 'xsi:schemaLocation', 'http://www.akadia.com/xml/soug/xmldom/emp.xsd'); root_node := xmldom.appendChild( main_node , xmldom.makeNode(root_elmt) ); FOR get_users_rec IN get_users(10) LOOP -- create user element with rownum as attribute item_elmt := xmldom.createElement( doc , 'EMP' ); xmldom.setAttribute( item_elmt , 'num' , get_users_rec.rownum ); user_node := xmldom.appendChild( root_node

Page 20: XML Creation Using Plsql

, xmldom.makeNode(item_elmt) ); -- create user element: EMP_NO item_elmt := xmldom.createElement( doc , 'EMP_NO' ); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) ); item_text := xmldom.createTextNode( doc , get_users_rec.empno ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); -- create user element: NAME item_elmt := xmldom.createElement( doc , 'NAME' ); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) ); item_text := xmldom.createTextNode( doc , get_users_rec.ename ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); -- create user element: DEPT_NO item_elmt := xmldom.createElement( doc , 'DEPT_NO'); item_node := xmldom.appendChild( user_node , xmldom.makeNode(item_elmt) ); item_text := xmldom.createTextNode( doc , get_users_rec.deptno ); item_node := xmldom.appendChild( item_node , xmldom.makeNode(item_text) ); END LOOP; -- write document to file using default character set from database xmldom.writeToFile( doc , '/tmp/xml/docSample.xml'

Page 21: XML Creation Using Plsql

); -- free resources xmldom.freeDocument(doc);END;/

-- deal with exceptionsEXCEPTION

WHEN xmldom.INDEX_SIZE_ERR THEN raise_application_error(-20120, 'Index Size error');WHEN xmldom.DOMSTRING_SIZE_ERR THEN raise_application_error(-20120, 'String Size error');WHEN xmldom.HIERARCHY_REQUEST_ERR THEN raise_application_error(-20120, 'Hierarchy request error');WHEN xmldom.WRONG_DOCUMENT_ERR THEN raise_application_error(-20120, 'Wrong doc error');WHEN xmldom.INVALID_CHARACTER_ERR THEN raise_application_error(-20120, 'Invalid Char error');WHEN xmldom.NO_DATA_ALLOWED_ERR THEN raise_application_error(-20120, 'Nod data allowed error');WHEN xmldom.NO_MODIFICATION_ALLOWED_ERR THEN raise_application_error(-20120, 'No mod allowed error');WHEN xmldom.NOT_FOUND_ERR THEN raise_application_error(-20120, 'Not found error');WHEN xmldom.NOT_SUPPORTED_ERR THEN raise_application_error(-20120, 'Not supported error');WHEN xmldom.INUSE_ATTRIBUTE_ERR THEN raise_application_error(-20120, 'In use attr error');END;/