Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.26 MB, 774 trang )
Using XML Parser for PL/SQL Examples in the Sample Directory
-- 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);
20-14 Oracle9i XML Developer’s Kits Guide - XDK
Using XML Parser for PL/SQL Examples in the Sample Directory
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;
XML Parser for PL/SQL 20-15
Frequently Asked Questions About the XML Parser for PL/SQL
Frequently Asked Questions About the XML Parser for PL/SQL
Why Do I Get an "Exception in Thread" Parser Error?
When I try to use the oraxsl I get the following: Exception in thread main:
java.lang.NoClassDefFoundError" oracle/xml/parser/v2/oraxsl.
How do I fix this?
Answer: If you are running outside the database you need to make sure the
xmlparserv2.jar is explicitly in your CLASS_PATH, not simply its directory. If
from the database you need to make sure it has been properly loaded and that
JServer initialized.
How Do I Use the xmldom.GetNodeValue in PL/SQL?
I cannot get the element value using the PL/SQL XMLDOM. Here is the code
fragment:
...nl := xmldom.getElementsByTagName(doc, '*');
len := xmldom.getLength(nl)
;-- loop through elements
for i in 0..len-1 loop
n := xmldom.item(nl, i);
elename := xmldom.getNodeName(n);
eleval := xmldom.getNodeValue(n);
...elename is Ok, but eleval is NULL.
Associating with a text node does not seem to work, or I am not doing it correctly? I
receive a compile error, as in this example:
...t xmldom.DOMText;
...t := xmldom.makeText(n);
eleval := xmldom.getNodeValue(t);
What am I doing wrong?
Answer: To get the text node value associated with the element node, you must
perform additional node navigation through xmldom.getFirstChild(n).
To illustrate, change printElements() in DOMSample.sql as follows:
begin
-- get all elements
nl := xmldom.getElementsByTagName(doc, '*');
len := xmldom.getLength(nl);
20-16 Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About the XML Parser for PL/SQL
-- loop through elements
for i in 0..len-1 loop
n := xmldom.item(nl, i);
dbms_output.put(xmldom.getNodeName(n));
-- get the text node associated with the element node
n := xmldom.getFirstChild(n);
if xmldom.getNodeType(n) = xmldom.TEXT_NODE then
dbms_output.put('=' | | xmldom.getNodeValue(n));
end if;
dbms_output.put(' ');
end loop;
dbms_output.put_line('');
end printElements;
This produces the following output, listing the elements:
family member=Sarah member=Bob member=Joanne member=Jim
The attributes of each element are:
family:familylastname val=Smithmember:membermemberid val=m1member:membermemberid
val=m2member:membermemberid val=m3 mom val=m1 dad val=m2member:membermemberid
val=m4 mom val=m1 dad val=m2
Can I Run the XDK for PL/SQL in an IIS Environment?
I downloaded XDK for PL/SQL but it requires OAS. Do you have any idea how to
run this in an IIS environment?
Answer: If you're going to use IIS, it would be better to use the XML Parser for Java
version 2. You'll also need Oracle9i.
How Do I Parse a DTD Contained in a CLOB with the XML Parser for PL/SQL?
I am having problems parsing a DTD file contained in a CLOB. I used the
xmlparser.parseDTDClob API, provided by the XML Parser for PL/SQL.
I received the following error:
"ORA-29531: no method parseDTD in class oracle/xml/parser/plsql/XMLParserCover".
The procedure xmlparser.parseDTDClob calls a Java Stored Procedure
xmlparsercover.parseDTDClob, which in turn calls another Java Stored
Procedure xmlparsercover.parseDTD.
I have confirmed that the class file,
oracle.xml.parser.plsql.XMLParserCover, has been loaded into the
XML Parser for PL/SQL 20-17
Frequently Asked Questions About the XML Parser for PL/SQL
database, and that it has been published. So the error message does not make sense.
The procedure used to call xmlparser.parseDTDClob is:
create or replace procedure parse_my_dtd as p xmlparser.parser; l_clob clob;
begin p := xmlparser.newParser; select content into l_clob from
dca_documents where doc_id = 1;
xmlparser.parseDTDClob(p,l_clob,'site_template'); end; API Documentation for
xmlparser.parseDTDClob:
parseDTDClob PURPOSE Parses the DTD stored in the given clob SYNTAX
PROCEDURE parseDTDClob(p Parser, dtd CLOB, root VARCHAR2); PARAMETERS p
(IN)- parser instance dtd
(IN)- dtd clob to parse root
(IN)- name
of the root element RETURNS Nothing COMMENTS
Any changes to the default parser behavior should be made before calling this
procedure. An application error is raised if parsing failed, for some reason.
Description of the table dca_documents:
DOC_ID
NOT NULL NUMBER DOC_NAME
NOT NULL
DOC_TYPE
VARCHAR2(30)
DESCRIPTION
VARCHAR2(4000) MIME_TYPE
VARCHAR2(48) CONTENT
NOT NULL CLOB CREATED_BY
VARCHAR2(30) CREATED_ON
NOT NULL DATE UPDATED_BY
VARCHAR2(30) UPDATED_ON
NOT NULL DATE
VARCHAR2(350)
NOT NULL
NOT NULL
The contents of the DTD:
#REQUIRED>
component (#PCDATA)>
component parent_id ID #REQUIRED>
#REQUIRED>
Answer: This is a known issue in release 1.0.1 of the XML Parser for PL/SQL. Here
is the workaround.
First, make a backup of
./plsqlxmlparser_1.0.1/lib/sql/xmlparsercover.sql
Then, in line 18 of xmlparsercover.sql, change the string
oracle.xml.parser.plsql.XMLParserCover.parseDTD to
oracle.xml.parser.plsql.XMLParserCover.parseDTDClob
Verify that Line 18 now reads:
procedure parseDTDClob(id varchar2, DTD CLOB, root varchar2, err in out
20-18 Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About the XML Parser for PL/SQL
varchar2)
is language java name
'oracle.xml.parser.plsql.XMLParserCover.parseDTDClob(java.lang.String,
oracle.sql.CLOB, java.lang.String, java.lang.String[])';
Save the file, then rerun xmlparsercover.sql in SQL*Plus. Assuming you've
loaded XMLParser version 2 release 2.0.2.6 into the database, this should solve your
problem.
How Do I Use Local Variables with the XML Parser for PL/SQL?
I have just started using XML Parser for PL/SQL. I am have trouble getting the text
between the begin tag and the end tag into a local variable. Do you have examples?
Answer: You just have to use the following:
selectSingleNode("pattern");
getNodeValue()
Remember, if you are trying to get value from a Element node, you have to move
down to the #text child node, for example, getFirstChild.getNodeValue()
Suppose you need to get the text contained between the starting and ending tags of
a xmldom.DOMNode n. The following two lines will suffice.
n_child:=xmldom.getFirstChild(n);
text_value:=xmldom.getNodeValue(n_child));
n_child is of type xmldom.DOMNode.
text_value is of type varchar2.
Why Do I Get a Security Error When I Grant JavaSysPriv to a User?
We are using the XML Parser for PLSQL and are trying to parse an XML document.
We are getting a Java security error:
ORA-29532: Java call terminated by uncaught Java exception:
java.lang.SecurityException ORA-06512: at "NSEC.XMLPARSERCOVER", line 0
ORA-06512: at "NSEC.XMLPARSER", line 79 ORA-06512: at "NSEC.TEST1_XML line 36
ORA-06512: at line 5
Do we need to grant to user? The syntax appears correct. We also get the error when
we run the demo.
XML Parser for PL/SQL 20-19
Frequently Asked Questions About the XML Parser for PL/SQL
Answer: If the document you are parsing contains a doctype which has a System
URI with a protocol like file:/// or http:/// then you need to grant an
appropriate privilege to your current database user to be able to "reach out of the
database", so to speak, and open a stream on the file or URL.CONNECT
SYSTEM/MANAGER. The following code should do it:
GRANT JAVAUSERPRIV, JAVASYSPRIV TO youruser;
How Do I Install the XML Parser for PL/SQL with the JServer (JVM) Option?
I have downloaded and installed the plxmlparser_V1_0_1.tar.gz. The
readme said to use loadjava to upload xmlparserv2.jar and plsql.jar in
order. I tried to load xmlparserv2.jar using the following command:
loadjava -user test/test -r -v xmlparserv2.jar
to upload the jar file into Oracle8i. After much of the uploading, I got the following
error messages:
identical: oracle/xml/parser/v2/XMLConstants is unchanged from previously loaded
fileidentical: org/xml/sax/Locator is unchanged from previously loaded
fileloading : META-INF/MANIFEST.MFcreating : META-INF/MANIFEST.MFError while
creating resource META-INF/MANIFEST.MF
ORA-29547: Java system class not
available: oracle/aurora/rdbms/Compilerloading :
oracle/xml/parser/v2/mesg/XMLErrorMesg_en_US.propertiescreating :
oracle/xml/parser/v2/mesg/XMLErrorMesg_en_US.propertiesError while creating
...
Then I removed -r from the previous command:
loadjava -user test/test -v xmlparserv2.jar
I still got errors but it's down to four:
.identical: org/xml/sax/Locator is unchanged from previously loaded fileloading
: META-INF/MANIFEST.MFcreating : META-INF/MANIFEST.MFError while creating
...
I think I have installed the JServer on the database correctly.
Answer: The JServer option is not properly installed if you're getting errors like this
during loadjava. You need to run INITJVM.SQL and INITDBJ.SQL to get the
JavaVM properly installed. Usually these are in the ./javavm subdirectory of your
Oracle Home.
20-20 Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About the XML Parser for PL/SQL
How Do I Use the domsample Included with XML Parser for PL/SQL?
I am trying to execute domsample on dom1151. This is an example that is provided
with the XML Parser for PL/SQL. The XML file family.xml is present in the
directory /hrwork/log/pqpd115CM/out.
I am getting the following error:
Usage of domsample is domsample(dir, inpfile, errfile)
SQL>
begin
domsample('/hrwork/log/pqpd115CM/out','family.xml','errors.txt');
end;
/
Error generated :
begin
*
ERROR at line 1:
ORA-20100: Error occurred while parsing: No such file or directory
ORA-06512: at "APPS.XMLPARSER", line 22
ORA-06512: at "APPS.XMLPARSER", line 69
ORA-06512: at "APPS.DOMSAMPLE", line 80
ORA-06512: at line 2
Answer: From your description it sounds like you have not completed all of the
steps in the sample and Readme without errors. After confirming that the
xmlparserv2.jar is loaded, carefully complete the steps again.
How Do I Extract Part of a CLOB?
In an Oracle8i database, we have CLOBs which contain well-formed XML
documents up to 1 MB in size.
We want the ability to extract only part of the CLOB (XML document), modify it,
and replace it back in the database rather than processing the entire document.
Second, we want this process to run entirely on the database tier.
Which products or tools are needed for this? This may be possible with the JVM
which comes with Oracle9i. There also may be some PL/SQL tools available to
achieve this by means of stored procedures.
Answer: You can do this by using either of the following:
s
Oracle XML Parser for PLSQL
XML Parser for PL/SQL 20-21
Frequently Asked Questions About the XML Parser for PL/SQL
s
Create your own custom Java stored procedure wrappers over some code you
write yourselves with the Oracle XML Parser for Java.
XML Parser for PLSQL has methods such as the following:
s
s
xmlparser.parseCLOB()
xslProcessor.selectNodes(), to find what part of the doc you are looking
for
s
xmldom.* methods to manipulate the content of the XML document
s
xmldom.writeToCLOB() to write it back
If you wanted to do fine-detail updates on the text of the CLOB, you would have to
use DBMS_LOB.* routines, but this would be tricky unless the changes being made
to the content don't involve any increase or decrease in the number of characters.
Why Do I Get "Out of Memory" Errors in the XML Parser?
We are parsing a 50Mb XML file. We have upped the java_pool_size to 150Mb with
a shared_pool_size of 200Mb. We get the following "out of memory" errors in the
Oracle XML parser:
last entry at 2000-04-26 10:59:27.042:
VisiBroker for Java runtime caught exception:
java.lang.OutOfMemoryError
at oracle.xml.parser.v2.XMLAttrList.put(XMLAttrList.java:251)
at oracle.xml.parser.v2.XMLElement.setAttribute(XMLElement.java:260)
at oracle.xml.parser.v2.XMLElement.setAttribute(XMLElement.java:228)
at cars.XMLServer.processEXL(XMLServer.java:122)
It's trying to create a new XML attribute and crashes with OutOfMemoryError.
Answer: You should not be using the DOM parser for parsing a 50Mb XML file. You
need to use the SAX parser, which parses files of arbitrary size because it does not
create an in-memory tree of nodes as it goes.
If you are using DOM, you should seriously consider moving to SAX which
processes the XML file sequentially instead of trying to build an in-memory tree
that represents the file.
Using SAX we process XML files in excess of 180Mb without any problems and
with very low memory requirements.
Rule of thumb for choosing between DOM and SAX:
DOM:
20-22 Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About Using the DOM API
s
DOM is very good when you need some sort of random access.
s
DOM consumes more memory.
s
DOM is also good when you are trying to transformations of some sort.
s
s
DOM is also good when you want to have tree iteration and want to walk
through the entire document tree.
See if you can use more attributes over elements in your XML (to reduce the
pipe size).
SAX:
s
SAX is good when data comes in a streaming manner (using some input
stream).
What Are the Memory Requirements for Using the PL/SQL Parser?
Answer: While the memory use is directly dependent on the document size, it
should also be realized that the PL/SQL parser uses the Java parser and thus the
Oracle JServer is being run. JServer typically requires 40-60 MB depending on its
configuration.
Is JServer (JVM) Needed to Run XML Parser for PL/SQL?
Answer: Yes, if you are running the parser in the database, you do need JServer
because the PL/SQL parser currently uses the XML Parser for Java under the
covers. JServer exists in both the Standard and Enterprise versions. A forthcoming
version of XML Parser for PL/SQL using C underneath is being developed for
applications that do not have access to a Java Virtual Machine (JVM).
Frequently Asked Questions About Using the DOM API
What Does the XML Parser for PL/SQL Do?
Answer: The XML parser accepts any XML document and gives you a tree-based
API (DOM) to access or modify the document’s elements and attributes. It also
supports XSLT which allows transformation from one XML document to another.
XML Parser for PL/SQL 20-23
Frequently Asked Questions About Using the DOM API
Can I Dynamically Set the Encoding in the XML Document?
Answer: No, you need to include the proper encoding declaration in your
document according to the specification. You cannot use
setCharset(DOMDocument) to set the encoding for the input of your document.
SetCharset(DOMDocument) is used with
oracle.xml.parser.v2.XMLDocument to set the correct encoding for the
printing.
How Do I Get the Number of Elements in a Particular Tag?
How do I get the number of elements in a tag using the Parser?
Answer: You can use the getElementByTagName (elem DOMElement, name
IN VARCHAR2) method that returns a DOMNodeList of all descent elements with a
given tag name. You can then find out the number of elements in that
DOMNodeList to determine the number of the elements in the particular tag.
How Do I Parse a String?
Answer: We do not currently have any method that can directly parse an XML
document contained within a string. You can use one of the following as a
workaround:
s
s
s
function parse (Parser, VARCHAR2) to parse XML data stored in the given
URL or the given file,
function parseBuffer (Parser, VARCHAR2) to parser XML data stored in
the given buffer, or
function parseCLOB (Parser, VARCHAR2) to parse XML data stored in the
give CLOB.
How Do I Display My XML Document?
Answer: If you are using Internet Explorer 5 as your browser, you can display the
XML document directly. Otherwise, you can use our XSLT processor in version 2 of
the parser to create the HTML document using an XSL Stylesheet. Our Java
Transviewer bean also enables you to view your XML document.
20-24 Oracle9i XML Developer’s Kits Guide - XDK