Oracle8i Application Developer's Guide - XML
Release 3 (8.1.7)

Part Number A86030-01

Library

Service

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Using XML Parser for PL/SQL, 6 of 7


Using XML Parser for PL/SQL Examples in sample/

Setting Up the Environment to Run the sample/ Sample Programs

The $ORACLE_HOME/xdk/plsql/parser/sample/ directory contains two sample XML applications:

These show you how to use XML Parser for PL/SQL.

To run these sample programs carry out the following steps:

  1. Load the PL/SQL parser into the database. To do this, follow the instructions given in the README file under the lib directory.

  2. You must have the appropriate java security privileges to read and write from a file on the file system. To this, first startup SQL*Plus (located typically under $ORACLE_HOME/bin) and connect as a user with admin privileges, such as, 'internal':

    For example

    % sqlplus
    SQL> connect internal
    
    
  3. A password might be required for 'internal' or the appropriate user with admin privileges. Contact your System Administrator, DBA, or Oracle support, if you cannot login with admin privileges.

  4. Give special privileges to the user running this sample. It must be the same one under which you loaded the jar files and plsql files in Step 1.

    For example, for user 'scott':

    SQL> grant javauserpriv to scott;
    SQL> grant javasyspriv to scott;
    
    

    You should see two messages that say "Grant succeeded." Contact your System Administrator, DBA, or Oracle support, if this does not occur.

    Now, connect again as the user under which the PL/SQL parser was loaded in step 1. For example, for user 'scott' with password 'tiger':

    SQL> connect scott/tiger
    

Running domsample

To run domsample carry out the following steps:

  1. Load domsample.sql script under SQL*Plus (if SQL*Plus is not up, first start it up, connecting as the user running this sample) as follows:

       SQL> @domsample
    
    

    The domsample.sql script defines a procedure domsample with the following syntax:

    domsample(dir varchar2, inpfile varchar2, errfile varchar2)
    
    

    where:

    Argument  Description 

    'dir' 

    Must point to a valid directory on the external file system and should be specified as a complete path name 

    'inpfile' 

    Must point to the file located under 'dir', containing the XML document to be parsed 

    'errfile' 

    Must point to a file you wish to use to record errors; this file will be created under 'dir' 

  2. Execute the domsample procedure inside SQL*Plus by supplying appropriate arguments for 'dir', 'inpfile', and 'errfile'. For example:

    On Unix, you can could do the following:

    SQL>execute domsample('/private/scott', 'family.xml', 'errors.txt');
    
    

    On Windows NT, you can do the following:

    SQL>execute domsample('c:\xml\sample', 'family.xml', 'errors.txt');
    
    

    where family.xml is provided as a test case

  3. You should see the following output:

    • The elements are: family member member member member

    • The attributes of each element are:

      family:
      lastname = Smith
        member:
        memberid = m1
        member:
        memberid = m2
        member:
        memberid = m3 mom = m1 dad = m2
        member:
        memberid = m4 mom = m1 dad = m2
      
      
      

Running xslsample

To run xslsample, carry out these steps:

  1. Load the xslsample.sql script under SQL*Plus (if SQL*Plus is not up, first start it up, connecting as the user running this sample):

    SQL>@xslsample
    
    

    xslsample.sql script defines a procedure xslsample with the following syntax:

    xslsample ( dir varchar2, xmlfile varchar2, xslfile varchar2, resfile 
    varchar2, errfile varchar2 )
    
    

    where:

    Argument  Description 

    'dir' 

    Must point to a valid directory on the external file system and should be specified as a complete path nam 

    'xmlfile' 

    Must point to the file located under 'dir', containing the XML document to be parsed 

    'xskfile' 

    Must point to the file located under 'dir', containing the XSL stylesheet to be applied 

    'resfile' 

    Must point to the file located under 'dir' where the transformed document is to be placed 

    'errfile' 

    Must point to a file you wish to use to record errors; this file will be created under 'dir' 

  2. Execute the xslsample procedure inside SQL*Plus by supplying appropriate arguments for 'dir', 'xmlfile', 'xslfile', and 'errfile'.

    For example:

    • On Unix, you can do the following:

      SQL>execute xslsample('/private/scott', 'family.xml', 'iden.xsl',                           
      'family.out', 'errors.txt');
      
      
    • On NT, you can do the following:

      SQL>execute xslsample('c:\xml\sample', 'family.xml', 'iden.xsl',                            
      'family.out', 'errors.txt');
      
      
  3. The provided test cases are: family.xml and iden.xsl

  4. You should see the following output:

    Parsing XML document c:\/family.xml
    Parsing XSL document c:\/iden.xsl
    XSL Root element information
    Qualified Name: xsl:stylesheet
    Local Name: stylesheet
    Namespace: http://www.w3.org/XSL/Transform/1.0
    Expanded Name: http://www.w3.org/XSL/Transform/1.0:stylesheet
    A total of 1 XSL instructions were found in the stylesheet
    Processing XSL stylesheet
    Writing transformed document
    
    
  5. family.out should contain the following:

    <family lastname="Smith">
    <member memberid="m1">Sarah</member>
    <member memberid="m2">Bob</member>
    <member memberid="m3" mom="m1" dad="m2">Joanne</member>
    <member memberid="m4" mom="m1" dad="m2">Jim</member>
    </family>
    
    

    You might see a delay in getting the output when executing the procedure for the first time. This is because Oracle JVM performs various initialization tasks before it can execute a Java Stored Procedure (JSP). Subsequent invocations should run quickly.

    If you get errors, ensure the directory name is specified as a complete path on the file system


    Note:

    SQL directory aliases and shared directory syntax '\\' are not supported at this time.  


    Otherwise, report the problem on the XML discussion forum at http://technet.oracle.com

XML Parser for PL/SQL Example 1: XML -- family.xml

This XML file inputs domsample.sql.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE family SYSTEM "family.dtd">
<family lastname="Smith">
<member memberid="m1">Sarah</member>
<member memberid="m2">Bob</member>
<member memberid="m3" mom="m1" dad="m2">Joanne</member>
<member memberid="m4" mom="m1" dad="m2">Jim</member>
</family>

XML Parser for PL/SQL Example 2: DTD -- family.dtd

This DTD file is referenced by XML file, family.xml.

<!ELEMENT family (member*)>
<!ATTLIST family lastname CDATA #REQUIRED>
<!ELEMENT member (#PCDATA)>
<!ATTLIST member memberid ID #REQUIRED>
<!ATTLIST member dad IDREF #IMPLIED>
<!ATTLIST member mom IDREF #IMPLIED>

XML Parser for PL/SQL Example 3: XSL -- iden.xsl

This XSL file inputs the xslsample.sql.

<?xml version="1.0"?> 

<!-- Identity transformation -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:template match="*|@*|comment()|processing-instruction()|text()">
  <xsl:copy>
  <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
  </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

XML Parser for PL/SQL Example 4: PL/SQL -- domsample.sql

-- This file demonstrates a simple use of the parser and DOM API.
-- The XML file that is given to the application is parsed and the
-- elements and attributes in the document are printed.
-- It shows you how to set the parser options.

set serveroutput on;
create or replace procedure domsample(dir varchar2, inpfile varchar2, 
                                      errfile varchar2) is
p xmlparser.parser;
doc xmldom.DOMDocument;

-- prints elements in a document
procedure printElements(doc xmldom.DOMDocument) is
nl xmldom.DOMNodeList;
len number;
n xmldom.DOMNode;

begin
   -- get all elements
   nl := xmldom.getElementsByTagName(doc, '*');
   len := xmldom.getLength(nl);

   -- loop through elements
   for i in 0..len-1 loop
      n := xmldom.item(nl, i);
      dbms_output.put(xmldom.getNodeName(n) || ' ');
   end loop;

   dbms_output.put_line('');
end printElements;

-- prints the attributes of each element in a document
procedure printElementAttributes(doc xmldom.DOMDocument) is
nl xmldom.DOMNodeList;
len1 number;
len2 number;
n xmldom.DOMNode;
e xmldom.DOMElement;
nnm xmldom.DOMNamedNodeMap;
attrname varchar2(100);
attrval varchar2(100);

begin

   -- get all elements
   nl := xmldom.getElementsByTagName(doc, '*');
   len1 := xmldom.getLength(nl);

   -- loop through elements
   for j in 0..len1-1 loop
      n := xmldom.item(nl, j);
      e := xmldom.makeElement(n);
      dbms_output.put_line(xmldom.getTagName(e) || ':');

      -- get all attributes of element
      nnm := xmldom.getAttributes(n);

     if (xmldom.isNull(nnm) = FALSE) then
        len2 := xmldom.getLength(nnm);

        -- loop through attributes
        for i in 0..len2-1 loop
           n := xmldom.item(nnm, i);
           attrname := xmldom.getNodeName(n);
           attrval := xmldom.getNodeValue(n);
           dbms_output.put(' ' || attrname || ' = ' || attrval);
        end loop;
        dbms_output.put_line('');
     end if;
   end loop;

end printElementAttributes;

begin

-- new parser
   p := xmlparser.newParser;

-- set some characteristics
   xmlparser.setValidationMode(p, FALSE);
   xmlparser.setErrorLog(p, dir || '/' || errfile);
   xmlparser.setBaseDir(p, dir);

-- parse input file
   xmlparser.parse(p, dir || '/' || inpfile);

-- get document
   doc := xmlparser.getDocument(p);

-- Print document elements
   dbms_output.put('The elements are: ');
   printElements(doc);

-- Print document element attributes
   dbms_output.put_line('The attributes of each element are: ');
   printElementAttributes(doc);

-- deal with exceptions
exception

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 domsample;
/
show errors;

XML Parser for PL/SQL Example 5: PL/SQL -- xslsample.sql

-- This file demonstates a simple use of XSL-T transformation capabilities.
-- The XML and XSL files that are given to the application are parsed, 
-- the transformation specified is applied and the transformed document is 
-- written to a specified result file.
-- It shows you how to set the parser options.

set serveroutput on;
create or replace procedure xslsample(dir varchar2, xmlfile varchar2, 
                                      xslfile varchar2, resfile varchar2, 
                                      errfile varchar2) is
p xmlparser.Parser;
xmldoc xmldom.DOMDocument;
xmldocnode xmldom.DOMNode;
proc xslprocessor.Processor;
ss xslprocessor.Stylesheet;
xsldoc xmldom.DOMDocument;
docfrag xmldom.DOMDocumentFragment;
docfragnode xmldom.DOMNode;
xslelem xmldom.DOMElement;
nspace varchar2(50);
xslcmds xmldom.DOMNodeList;

begin

-- new parser
   p := xmlparser.newParser;

-- set some characteristics
   xmlparser.setValidationMode(p, FALSE);
   xmlparser.setErrorLog(p, dir || '/' || errfile);
   xmlparser.setPreserveWhiteSpace(p, TRUE);
   xmlparser.setBaseDir(p, dir);

-- parse xml file
   dbms_output.put_line('Parsing XML document ' || dir || '/' || xmlfile);
   xmlparser.parse(p, dir || '/' || xmlfile);

-- get document
   xmldoc := xmlparser.getDocument(p);

-- parse xsl file
   dbms_output.put_line('Parsing XSL document ' || dir || '/' || xslfile);
   xmlparser.parse(p, dir || '/' || xslfile);

-- get document
   xsldoc := xmlparser.getDocument(p);

   xslelem := xmldom.getDocumentElement(xsldoc);
   nspace := xmldom.getNamespace(xslelem);

-- print out some information about the stylesheet
   dbms_output.put_line('XSL Root element information');
   dbms_output.put_line('Qualified Name: ' || 
                         xmldom.getQualifiedName(xslelem));
   dbms_output.put_line('Local Name: ' || 
                         xmldom.getLocalName(xslelem));
   dbms_output.put_line('Namespace: ' || nspace);
   dbms_output.put_line('Expanded Name: ' || 
                         xmldom.getExpandedName(xslelem));

   xslcmds := xmldom.getChildrenByTagName(xslelem, '*', nspace);
   dbms_output.put_line('A total of ' || xmldom.getLength(xslcmds) || 
                        ' XSL instructions were found in the stylesheet');
-- make stylesheet
   ss := xslprocessor.newStylesheet(xsldoc, dir || '/' || xslfile);

-- process xsl
   proc := xslprocessor.newProcessor;
   xslprocessor.showWarnings(proc, true);
   xslprocessor.setErrorLog(proc, dir || '/' || errfile);

   dbms_output.put_line('Processing XSL stylesheet');
   docfrag := xslprocessor.processXSL(proc, ss, xmldoc);
   docfragnode := xmldom.makeNode(docfrag);
 
   dbms_output.put_line('Writing transformed document');
  xmldom.writeToFile(docfragnode, dir || '/' || resfile); 

-- deal with exceptions
exception

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 xslsample;
/
show errors;


Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Service

Contents

Index