Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
|
Using XML Parser for PL/SQL, 6 of 7
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:
For example
% sqlplus SQL> connect internal
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
To run domsample carry out the following steps:
SQL> @domsample
The domsample.sql
script defines a procedure domsample with the following syntax:
domsample(dir varchar2, inpfile varchar2, errfile varchar2)
where:
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
To run xslsample, carry out these steps:
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:
xslsample
procedure inside SQL*Plus by supplying appropriate arguments for 'dir', 'xmlfile', 'xslfile', and 'errfile'.
For example:
family.xml
and iden.xsl
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
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
Otherwise, report the problem on the XML discussion forum at http://technet.oracle.com
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>
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>
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>
-- 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;
-- 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;
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|