Is there a SAX parser with PL/SQL??
Hi All,
I am a Oracle developer and generally want to do everything from PL/SQL.
So, from a PL/SQL procedure I would like to parse a XML document. I have tried hard to find a PL/SQL package which can give me a PL/SQL SAX parser, but could not find one.
All packages are based on DOM tree.
Please convey if I am wrong and is there a SAX parser?
I know that there is only one package DBMS_XMLSTORE which is C based and uses SAX parser, but it does not have procedures/functions like ".parse()".
Thanks and regards
Here's an example
SQL> --
SQL> define MODULE=SaxProcessor
SQL> define CLASS=com/oracle/st/xmldb/pm/examples/SaxProcessor
SQL> --
SQL> set define off
SQL> --
SQL> var sourceFileName varchar2(32)
SQL> var targetPath varchar2(1024)
SQL> --
SQL> begin
2 :sourceFileName := 'JavaSource';
3 :targetPath := '/public/' || :sourceFileName || '.java';
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> declare
2 res boolean;
3 javaSource clob :=
4 'package com.oracle.st.xmldb.pm.examples;
5
6
7 import java.io.IOException;
8 import java.io.StringWriter;
9
10 import java.io.Writer;
11
12 import java.sql.DriverManager;
13 import java.sql.SQLException;
14
15 import java.util.Enumeration;
16 import java.util.Hashtable;
17
18 import oracle.jdbc.OracleConnection;
19 import oracle.jdbc.OracleDriver;
20
21 import oracle.jdbc.OraclePreparedStatement;
22
23 import oracle.sql.BFILE;
24
25 import oracle.sql.CLOB;
26
27 import oracle.xml.parser.v2.SAXParser;
28 import oracle.xml.parser.v2.XMLDocument;
29 import oracle.xml.parser.v2.XMLElement;
30
31
32 import org.w3c.dom.Attr;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Node;
35
36 import org.xml.sax.Attributes;
37 import org.xml.sax.ContentHandler;
38 import org.xml.sax.Locator;
39 import org.xml.sax.SAXException;
40
41 public class SaxProcessor implements ContentHandler {
42
43 public static final boolean DEBUG = true;
44
45 private OracleConnection dbConnection;
46
47 private OraclePreparedStatement insertStatement;
48 private OraclePreparedStatement errorStatement;
49 private CLOB clob;
50
51 private Hashtable namespaceToPrefix = null;
52 private Hashtable prefixToNamespace = null;
53 private String targetElementName = null;
54
55 private XMLDocument currentDocument;
56 private Node currentNode;
57
58 private int documentCount = 0;
59
60 public SaxProcessor() {
61 this.namespaceToPrefix = new Hashtable();
62 this.prefixToNamespace = new Hashtable();
63 }
64
65 private boolean isTargetElement(String elementName) {
66 return ((this.currentDocument == null) &&
67 (elementName.equals(this.targetElementName)));
68 }
69
70 public void startDocument() throws SAXException {
71 }
72
73 public void endDocument() throws SAXException {
74 }
75
76 public void startElement(String namespaceURI, String localName, String elementName, Attributes attrs)
77 throws SAXException
78 {
79 if (DEBUG) {
80 System.out.println("startElement() : URI = " + namespaceURI + ", localName = " + localName + ", elementName = " + elementNa
me);
81 }
82 if (this.currentDocument == null)
83 {
84 if (DEBUG) {
85 System.out.println("startElement() : Checking for start of Fragment.");
86 }
87 if (isTargetElement(localName))
88 {
89 if (DEBUG) {
90 System.out.println("startElement() : Starting New Document");
91 }
92 this.currentDocument = new XMLDocument();
93 this.currentNode = this.currentDocument;
94 XMLElement rootElement = createNewElement(namespaceURI, localName, elementName, attrs);
95 this.currentDocument.appendChild(rootElement);
96 this.currentNode = rootElement;
97 }
98 }
99 else
100 {
101 XMLElement nextElement = createNewElement(namespaceURI, localName, elementName, attrs);
102 this.currentNode.appendChild(nextElement);
103 this.currentNode = nextElement;
104 }
105 }
106
107 public void endElement(String namespaceURI, String localName, String qName)
108 throws SAXException
109 {
110 if (this.currentDocument != null)
111 {
112 if (this.currentNode.equals(this.currentDocument.getDocumentElement()))
113 {
114 try
115 {
116 insertDocument();
117 this.currentDocument = null;
118 }
119 catch (SQLException sqlE)
120 {
121 throw new SAXException(sqlE);
122 }
123 catch (IOException ioe)
124 {
125 throw new SAXException(ioe);
126 }
127 }
128 else
129 {
130 this.currentNode = this.currentNode.getParentNode();
131 }
132 }
133 }
134
135 private XMLElement createNewElement(String namespaceURI, String localName,
136 String elementName, Attributes attrs) {
137 XMLElement newElement = null;
138 if (namespaceURI != null) {
139 if (this.namespaceToPrefix.containsKey(namespaceURI)) {
140 /* Namespace in already in Scope - create Element from Qualified Name */
141 newElement =
142 (XMLElement)this.currentDocument.createElement(elementName);
143 } else {
144 /* Namespace is not already in Scope - create Element with namespace */
145 newElement =
146 (XMLElement) this.currentDocument.createElementNS(namespaceURI,
147 elementName);
148 newElement.setPrefix((String)this.namespaceToPrefix.get(namespaceURI));
149 }
150 } else {
151 newElement =
152 (XMLElement)this.currentDocument.createElement(localName);
153 }
154 addAttributes(newElement, attrs);
155 if (this.currentNode.equals(this.currentDocument)) {
156 addNamespaceDeclarations(newElement);
157 }
158 return newElement;
159 }
160
161 private void addAttributes(Element element, Attributes attrs) {
162 for (int i = 0; i < attrs.getLength(); i++) {
163 if (attrs.getURI(i).equals("http://www.w3.org/2000/xmlns/")) {
164 } else {
165 element.setAttribute(attrs.getQName(i), attrs.getValue(i));
166 }
167 }
168 }
169
170 private void addNamespaceDeclarations(Element element) {
171 Enumeration keys = this.namespaceToPrefix.keys();
172 while (keys.hasMoreElements()) {
173 String namespace = (String)keys.nextElement();
174 String prefix = (String)namespaceToPrefix.get(namespace);
175 Attr attr = null;
176 if (prefix.equals("")) {
177 attr = this.currentDocument.createAttribute("xmlns");
178 attr.setValue(namespace);
179 element.setAttributeNode(attr);
180 } else {
181 if (!prefix.equals(element.getPrefix())) {
182 attr =
183 this.currentDocument.createAttribute("xmlns:" + prefix);
184 attr.setValue(namespace);
185 element.setAttributeNode(attr);
186 }
187 }
188 }
189 }
190
191 public void characters(char[] p0, int p1, int p2) throws SAXException {
192 if (this.currentDocument != null) {
193 StringWriter sw = new StringWriter();
194 sw.write(p0, p1, p2);
195 String value = sw.toString();
196 Node textNode = this.currentDocument.createTextNode(value);
197 this.currentNode.appendChild(textNode);
198 }
199 }
200
201 public void startPrefixMapping(String prefix,
202 String uri) throws SAXException {
203 this.namespaceToPrefix.put(uri, prefix);
204 this.prefixToNamespace.put(prefix, uri);
205 }
206
207 public void endPrefixMapping(String prefix) throws SAXException {
208 Enumeration e = prefixToNamespace.keys();
209 while (e.hasMoreElements()) {
210 String thisPrefix = (String)e.nextElement();
211 if (thisPrefix.equals(prefix)) {
212 String namespace =
213 (String)prefixToNamespace.remove(thisPrefix);
214 namespaceToPrefix.remove(namespace);
215 }
216 }
217 }
218
219 public void ignorableWhitespace(char[] p0, int p1,
220 int p2) throws SAXException {
221 // throw new SAXException ("Un-Implemented Method: ingnoreableWhitespace");
222 }
223
224 public void processingInstruction(String p0,
225 String p1) throws SAXException {
226 throw new SAXException("Un-Implemented Method: processingInstruction");
227 }
228
229 public void setDocumentLocator(Locator p0) {
230 // throw new SAXException ("Un-Implemented Method: setDocumentLocator");
231 }
232
233 public void skippedEntity(String p0) throws SAXException {
234 throw new SAXException("Un-Implemented Method: skippedEntity");
235 }
236
237 public void doParse(BFILE bfile, String targetElement,
238 String targetTable, String errorTable) throws Exception {
239 this.targetElementName = targetElement;
240 String insertStatementText =
241 "insert into " + targetTable + " values (xmlParse(DOCUMENT ? WELLFORMED))";
242 String errorStatementText =
243 "insert into " + errorTable + " values (xmlParse(DOCUMENT ? WELLFORMED))";
244
245 DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
246 OracleDriver ora = new OracleDriver();
247 this.dbConnection = (OracleConnection)ora.defaultConnection();
248
249 this.insertStatement =
250 (OraclePreparedStatement)this.dbConnection.prepareStatement(insertStatementText);
251 this.errorStatement =
252 (OraclePreparedStatement)this.dbConnection.prepareStatement(errorStatementText);
253
254 this.clob =
255 CLOB.createTemporary(this.dbConnection, true, CLOB.DURATION_SESSION);
256
257 SAXParser parser = new SAXParser();
258 parser.setAttribute(SAXParser.STANDALONE, Boolean.valueOf(true));
259 parser.setValidationMode(SAXParser.NONVALIDATING);
260 parser.setContentHandler(this);
261 bfile.openFile();
262 parser.parse(bfile.getBinaryStream());
263 bfile.closeFile();
264 this.insertStatement.close();
265 this.errorStatement.close();
266 }
267
268 private void insertDocument() throws SQLException, IOException {
269 this.clob.truncate(0);
270 Writer out = clob.setCharacterStream(0);
271 this.currentDocument.print(out);
272 out.close();
273
274 this.insertStatement.setClob(1, clob);
275 this.insertStatement.execute();
276
277 this.documentCount++;
278
279 if (DEBUG) {
280 System.out.println("insertDocument() : Document Inserted");
281 }
282 }
283
284 public static int parseBFile(BFILE bfile, String targetElement,
285 String targetTable, String errorTable) throws Exception {
286 try {
287 SaxProcessor processor = new SaxProcessor();
288 processor.doParse(bfile,targetElement,targetTable,errorTable);
289 return processor.documentCount;
290 }
291 catch (Exception e) {
292 e.printStackTrace(System.out);
293 throw e;
294 }
295
296 }
297 }
298 ';
299 begin
300 if dbms_xdb.existsResource(:targetPath) then
301 dbms_xdb.deleteResource(:targetPath);
302 end if;
303 res := dbms_xdb.createResource(:targetPath,javaSource);
304 end;
305 /
Queuing DELETE Event
PL/SQL procedure successfully completed.
SQL> --
SQL> set define on
SQL> --
SQL> create or replace and resolve java source
2 named "&MODULE"
3 using blob
4 (
5 select xdburiType('/public/JavaSource.java').getBlob(nls_charset_id('WE8ISO8859P1'))
6 from dual
7 )
8 /
old 2: named "&MODULE"
new 2: named "SaxProcessor"
Java created.
SQL> show errors
No errors.
SQL> --
SQL> declare
2 shortname varchar2(128);
3 begin
4 shortname := dbms_java.shortname('&CLASS');
5 execute immediate 'grant execute on "' || shortname || '" to public';
6 end;
7 /
old 4: shortname := dbms_java.shortname('&CLASS');
new 4: shortname := dbms_java.shortname('com/oracle/st/xmldb/pm/examples/SaxProcessor');
PL/SQL procedure successfully completed.
SQL> create or replace package SAX_PROCESSOR
2 as
3 procedure PARSE_BFILE(file BFILE, targetElement varchar2, targetTable varchar2, errorTable varchar2);
4 end;
5 /
Package created.
SQL> show errors
No errors.
SQL> --
SQL> create or replace package body SAX_PROCESSOR
2 as
3 --
4 procedure PARSE_BFILE(file BFILE, targetElement varchar2, targetTable varchar2, errorTable varchar2)
5 AS LANGUAGE JAVA
6 NAME 'com.oracle.st.xmldb.pm.examples.SaxProcessor.parseBFile( oracle.sql.BFILE, java.lang.String, java.lang.String, java.lang.Strin
g)';
7 end;
8 /
Package body created.
SQL> show errors
No errors.
SQL> --
SQL> drop table PO_TEST
2 /
Table dropped.
SQL> create table PO_TEST of XMLTYPE
2 /
Table created.
SQL> drop table PO_ERROR
2 /
Table dropped.
SQL> create table PO_ERROR of XMLTYPE
2 /
Table created.
SQL> create or replace directory xmldir as 'c:\temp'
2 /
Directory created.
SQL> set serveroutput on
SQL> --
SQL> call SAX_PROCESSOR.PARSE_BFILE(bfilename('XMLDIR','testcase.xml'),'PurchaseOrder','PO_TEST','PO_ERROR')
2 /
call SAX_PROCESSOR.PARSE_BFILE(bfilename('XMLDIR','testcase.xml'),'PurchaseOrder','PO_TEST','PO_ERROR')
ERROR at line 1:
ORA-29549: class XFILES.com/oracle/st/xmldb/pm/examples/SaxProcessor has
changed, Java session state cleared
SQL> call SAX_PROCESSOR.PARSE_BFILE(bfilename('XMLDIR','testcase.xml'),'PurchaseOrder','PO_TEST','PO_ERROR')
2 /
Call completed.
SQL> select count(*) from PO_TEST
2 /
3
SQL> select * from PO_TEST
2 /
<PurchaseOrder xsi:noNamespaceSchemaLocation="http://xfiles:8080/home/SCOTT/poSo
urce/xsd/purchaseOrder.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
">
<Reference>AMCEWEN-20030409123336271PDT</Reference>
<Actions>
<Action>
<User>KPARTNER</User>
</Action>
</Actions>
<Reject/>
<Requestor>Allan D. McEwen</Requestor>
<User>AMCEWEN</User>
<PurchaseOrder xsi:noNamespaceSchemaLocation="http://xfiles:8080/home/SCOTT/poSo
urce/xsd/purchaseOrder.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
">
<Reference>SKING-20030409123336321PDT</Reference>
<Actions>
<Action>
<User>SKING</User>
</Action>
</Actions>
<Reject/>
<Requestor>Steven A. King</Requestor>
<User>SKING</User>
<PurchaseOrder xsi:noNamespaceSchemaLocation="http://xfiles:8080/home/SCOTT/poSo
urce/xsd/purchaseOrder.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
">
<Reference>SMCCAIN-20030409120030451PDT</Reference>
<Actions>
<Action>
<User>SVOLLMAN</User>
</Action>
</Actions>
<Reject/>
<Requestor>Samuel B. McCain</Requestor>
<User>SMCCAIN</User>
SQL>
Similar Messages
-
For a long time on technet the
PL/SQL parser release notes have
said that they will be supporting
SAX in a future release. Is there
any idea of when this support may
materialize? Or is Java the only
option for a parser in the db for
the near future that can handle large
documents?
nullWhat would you want to use as the implementation of the SAX2 ContentHandler interface in PL/SQL? Stored procedure-based callbacks? Just curious how you would envision this working. Your input is valuable to our future thinking in this area. Most teams using PL/SQL that want to use SAX have written Java Stored Procedures that use SAX internally inside the stored procedure (written in Java) but are not doing the SAX parsing with PL/SQL-based callbacks.
-
DBMS_SQL.PARSE with PL/SQL types
Hi
I need to use DBMS_SQL.PARSE with PL/SQL types defined in a package.
I tried with a type record in a declare ..begin..end script but I got an error ..(on second parameter):
DBMS_SQL.PARSE(cursor_name, XXXXX, DBMS_SQL.NATIVE);
It's possible?
WIth SQL types defined at schema level it's works (es. Objects types) .
If it's not possible, how can I resolve?
StefanoAgain, please post what XXXXX is. In order to use package declared types:
SQL> create or replace
2 package pkg1
3 is
4 type emp_rec
5 is record (
6 empno number,
7 ename varchar2(10)
8 );
9 type emp_rec_tbl
10 is
11 table of emp_rec
12 index by pls_integer;
13 g_emp_rec_tbl pkg1.emp_rec_tbl;
14 end;
15 /
Package created.
SQL> declare
2 v_cur integer;
3 v_sql varchar2(1000);
4 begin
5 v_cur := dbms_sql.open_cursor(2);
6 v_sql := 'begin
7 select empno,
8 ename
9 bulk collect
10 into pkg1.g_emp_rec_tbl
11 from emp
12 where job = ''CLERK'';
13 end;';
14 dbms_sql.parse(
15 v_cur,
16 v_sql,
17 dbms_sql.native
18 );
19 end;
20 /
PL/SQL procedure successfully completed.
SQL>
SY. -
Trying to use XML SAX parser with JDK2 ...
Hi,
I'm pretty new to Java.
I'm trying to write and use java sample using XML SAX parser. I try with XP and XML4J.
Each time I compile it give me a error message like
"SAX01.java:23: unreported exception java.lang.Exception; must be caught or declared to be
thrown
(new SAX01()).countBooks();
^
Note: SAX01.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error"
For what I found, it seems that "deprecated" mean usage of old classes or methods. What should I do ?
Wait for the XML parser to be rewrite ? ....
Thank's
Constant"SAX01.java:23: unreported exception
java.lang.Exception; must be caught or declared to be
thrown
(new SAX01()).countBooks();
^Do this
public static void main(String[] args) throws Exception {
or do this
try
first part of expression?(new SAX0()).countBooks();
catch(Exception ex)
System.out.println("problem "+ ex);
Note: SAX01.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error"deprecation is just a warning if there are no errors elsewhere in your program it should compile fine, but if you want to find out how to change it do this:
javac YourClassName.java -deprecation
then look in the docs for an alternative. -
How to stop the sax parser with parsing
hi,
I'm parsing some large xml-files with jaxp sax.
To do it I use a contenthandler that goes through the file.
Wat I want now is that when I have found the nodes that I need that parsing of the xml stops.
I tried to do it with detecting a certain endElement and putting there a return, but it will not work, parsing continues till the end of the file (see code underneath).
Does anyone can say what I'm doing wrong,
thanks, Hans
code:
public void endElement(String namespaceURI,
String sName,
String qName
) throws SAXException {
if (qName.equals("file")) {
return;
} // end of if ()
}You have to return from that method anyway, so a return statement is not going to tell the parser to stop. You could throw a SAXException, but be prepared to catch that in the code that called the parser, and be prepared to treat it differently from SAXExceptions that indicate problems.
-
hi,
is there a way to start parsing xml file with sax using offest, the file i am parsing is over 2gb and its parsed on multiple machines, each machine parsing 1/n (number of machines) of the file, i managed to do this simply by counting starting element of a record and if criteria is met records are parsed, tho for the last part parser need to read whole file and when it gets to desired position it starts parsing, is there a way to do this using some kind of offset. total number of records is known.
tnxhmhmmh, ok so here is the hole problem, this is all done and working properly, i just want to speed it up if possible.
in the beginning parsing was done on a single machine and since file is pretty big it took a lot of time, so we have made changes to code allowing it to be started from several machines and each one its doing one part of the same file (each instance has its copy), logic is: since number of records inside xml is known start and stop marker are calculated based on computer id and total number of computers on which parsing is done plus some safeties to ensure that every record is processed. so each instance of app knows when to start and stop parsing and simply goes through the file and counts number of records seen and when it reaches element representing start marker parsing is started and data sent where needed. parsing is done when sax reaches stop marker.
now i want to speed process of locating start marker since for the ie computer that is processing last part of the file app must go through entire file. i was asking if there is a way to tell sax where to "enter" the file so to say(ie 155th element, or in byte size, to simply move pointer to the xxxx byte)
and i need help on this matter since i have no idea how it can be done -
Problem Using Sax parser with Eclipse 3.0 and command line
Hi,
I am parsing a xml file with sax. When I am running my programm in the command line everthing is ok and I get the right results from parsing.
But if I am running the programm in Eclipse 3.0 (the same java code) I get an other result (the wrong results).
Does anybody know what this can be the reason for. Is Eclipse using an other xml parser and if where I can change the parser?
It would be very kind if somebody can give me a reason for this strange behaviour.
Thanks in advanceI have solved my problem.
In the command line I used jre 1.4 and in Eclipse I used jre 1.5.
I think jre 1.5 uses an other xml parser so I got an other result.
If i use in Eclipse jre1.4 I get the same result as in the command line. -
Problem with SAX parser with String format
hi, all I have the next problem:
I try to parse xml file with success, I write next code:
====================
my_class saxUms = new my_class();
File file = new File( "c:\\my_try_Response.xml" );
InputSource src1 = new InputSource( new FileInputStream( file ) );
XMLReader rdr = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser" ); rdr.setContentHandler( saxUms );
rdr.parse(src1);
===================
but when I try to parse the same in string variable, I write:
===================
my_class saxUms = new my_class();
StringReader strr = new StringReader(my_str);
InputSource intt = new InputSource(strr);
XMLReader prs= XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser" );
prs.setContentHandler(saxUms);
prs.parse(intt);
===================
and error occurs:
"Exception: java.lang.ClassCastException: java.lang.StringBuffer"
how to fix the problem?
Thank you for collaboration!where does the exception stack trace say the error is occurring?
-
Does anyone know why parsing with PL/SQL parser takes too long
I am parsing a XML document ( 168 K )
which it contains 300 Nodes and each node has 22 attributes, it
takes 5 minuts using xmldom calls ( from PL/SQL XML parser )..
When I cut that file in to 2 files ( 85 K or so ) it takes 1
minute per file..
Any ideas why ??????
ThanksI am parsing a XML document ( 168 K )
which it contains 300 Nodes and each node has 22 attributes, it
takes 5 minuts using xmldom calls ( from PL/SQL XML parser )..
When I cut that file in to 2 files ( 85 K or so ) it takes 1
minute per file..
Any ideas why ??????
Thanks -
SAX Parser Validation with Schemas (C, C++ and JAVA)
We are currently using the Oracle XML Parser for C to parse and validate XML data using the SAX parser interface with validation turned on. We currently define our XML with a DTD, but would like to switch to schemas. However, the Oracle XML Parser 9.2.0.3.0 (C) only validates against a DTD, not a schema when using the SAX interface. Are there plans to add schema validation as an option? If so, when would this be available? Also, the same limitation appears to be true for C++ and JAVA. When will any of these provide SAX parsing with schema validation?
Thanks!
JohnWill get back to you after checked with development team...
-
Why are all the events in the XML SAX parser not activated?
Hi everyone,
I have written a mini server that parses XML files into SQL queries.
Below is a segment of my code;
try {
Class.forName( JDBC_DRIVER );
myConnection = DriverManager.getConnection( DATABASE_URL, "username", "password");
EventXMLParser myEXP = new EventXMLParser(directory, myConnection);
File[] xmlFiles = directory.listFiles();
for (File xmlFile : xmlFiles) {
myEXP.XMLtoDB(xmlFile);
outWriter.println("File:" + xmlFile.getName() + " DONE");
} catch (SQLException e) {
System.err.println("SQLException for establishing connection");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.err.println("CLASS NOT FOUND EXCEPTION HERE");
e.printStackTrace();
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
finally {
outWriter.println("PARSING COMPLETED");
outWriter.close();
}Where the constructor EventXMLParser constructs the following:
public EventXMLParser(File path, Connection connection) {
super();
try {
this.XMLpath = path;
this.db_connection = connection;
this.xr = XMLReaderFactory.createXMLReader();
this.XMLSAXhandler = new DefaultHandler(); //create a new own handler
this.xr.setContentHandler(XMLSAXhandler);
this.xr.setErrorHandler(XMLSAXhandler);
//System.out.println("DEBUG: db_connection is " + db_connection.toString());
catch (Exception e) {
System.out.println("Constructor Error!");
e.printStackTrace();
}Below are all my helper methods within EventXMLParser.java
public void XMLtoDB(String XMLpath) throws Exception {
try {
//Input
System.out.println("XMLpath is : " + XMLpath);
/*FileReader r = new FileReader(XMLpath); debug
InputSource in = new InputSource(r);
xr.parse(in);
xr.parse(XMLpath);
/* Note that while parsing, the end of each event, </event>
* will trigger sendSQL to execute the query on the database
catch (Exception e) {
throw new Exception("Error with XMLtoDB!! Exception: " + e);
public void sendSQL(Event event, Connection sql_connection) throws SQLException {
//JDBC part
try {
System.err.println("DEBUG sendSQL");
Statement sql_statement = sql_connection.createStatement();
ResultSet resultSet = sql_statement.executeQuery( event.toSQL() );
catch (SQLException e) {
e.printStackTrace();
/* Parsing XML
* From here onwards it's all designed for the SAX Parsing with different event calling methods
public void startDocument() {
System.err.println("Start Document");
public void endDocument() {
System.err.println("End Document");
public void startElement(String uri, String name, String qName, Attributes atts) {
CurrentElement= name;
System.out.println("This is parsing");
public void characters(char ch[], int start, int length) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
StringBuffer sb = new StringBuffer();
for (int i = start; i < start + length; i++)
sb.append(ch);
String content = sb.toString();
if (CurrentElement.equals("eid"))
temp.setEventID( (Integer.valueOf(content)).intValue() ) ;
else if (CurrentElement.equals("sd"))
temp.setShort_description(content);
else if (CurrentElement.equals("ld"))
temp.setLong_description(content);
else if ( (CurrentElement.equals("dt")))
temp.setDate_Time( formatter.parse(content, new ParsePosition(0)) );
else if (CurrentElement.equals("repeat"))
temp.setRepeat_pattern( (Integer.valueOf(content)).intValue() );
else if (CurrentElement.equals("valid"))
temp.setValid_period(content);
else if (CurrentElement.equals("status")) {
temp.setStatus( (Integer.valueOf(content)).intValue() );
else {}
public void endElement(String uri, String name, String qName) {
System.err.println("DEBUG" + temp.toString()); /*debug*/
if (name.equals("event")) {
try {
/*debug*/ temp.setUserID(1);
/*debug*/ System.err.println("DEBUG: " + temp.toString());
sendSQL(temp, db_connection);
//temp = new Event();
catch (SQLException e) {
System.err.println(e);
}//end catch
}//end try
Where event is a public class Event {
//fields
private int userID = 1; // = 1 only applies for testing
private int eventID;
private String short_description;
private String long_description;
private Date date_time = null;
private int repeat_pattern;
private String valid_period;
private int status; //1 for new, 0 for modification and -1 for delete
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//Constructors
//every event requires the following: userID eventID and short_Description
public Event(int uID, int eID, String shortDescrp) {
setUserID(uID);
setEventID(eID);
setShort_description(shortDescrp);
public Event(int uid, int eid, String sd,
String ld, Date d_t, int r_p, String v_p, int s) {
setUserID(uid);
setEventID(eid);
setShort_description(sd);
setLong_description(ld);
setDate_Time(d_t);
setRepeat_pattern(r_p);
setValid_period(v_p);
setStatus(s);
//set
public void setUserID (int x) { this.userID = x ;}
public void setEventID (int x) { this.eventID = x ;}
public void setShort_description (String x) { this.short_description = x ;}
public void setLong_description (String x) { this.long_description = x ;}
public void setDate_Time(Date x) { this.date_time = x ;}
public void setRepeat_pattern (int x) { this.repeat_pattern = x ;}
public void setValid_period (String x) { this.valid_period = x ;}
public void setStatus (int x) { this.status = x; }
//get
public int getUserID() { return this.userID;}
public int getEventID() { return this.eventID;}
public String getShort_description() { return this.short_description;}
public String getLong_description() { return this.long_description;}
public Date getDate_Time() { return this.date_time;}
public int getRepeat_pattern() { return this.repeat_pattern;}
public String getValid_period() { return this.valid_period;}
public int getStatus() { return this.status; }
//Event to SQL statements;
public String toSQL() {
StringBuffer sb = new StringBuffer();
///if ( status == 1) {
sb.append( "INSERT INTO events SET" );
sb.append( " userID = " + userID + ", ");
sb.append( "eventID = " + eventID + ", " );
sb.append( "short_description = " + "\'" + short_description + "\'" + ", "); //String
sb.append( "long_description = " + "\'" + long_description + "\'" + ", "); //String
sb.append( "date_time = " + "\'" + formatter.format(date_time) + "\'" + ", ");
sb.append( "repeat_pattern = " + repeat_pattern + ", " );
sb.append( "valid_period = " + "\'" + valid_period + "\'" ); //String
sb.append( ";");
//} else if ( status == 2) {
System.err.println(sb.toString());
return sb.toString();
} My question is: I have taken my SQL query generated by toSQL() method in events and it worked.
Here is the funny thing:
Everything is correct syntax wise: No complaints what soever
The mysql part works: Tested separately.
So I tend to think that the problem lies within the SAX parser. I have written SAX2 parsers on this machine before and they have worked too. I tried inserting println statements all over startElement endElement etc etc only to find out that the SAX parser did not call any of the methods that I overided!! Why is that so?
Can you guys spot where my SAX parser fails?I see.
I try to correct this problem by removing super();
so right now my code looks like this:
static Event temp = new Event(0, 0, "null", "null", new Date(), 0, "null", 0);
static String CurrentElement = null;
static File XMLpath;
static Connection db_connection;
static XMLReader xr;
static DefaultHandler XMLSAXhandler;
//Constructor, Build the SAX Parser
public EventXMLParser(File path, Connection connection) {
try {
this.XMLpath = path;
this.db_connection = connection;
this.xr = XMLReaderFactory.createXMLReader();
this.XMLSAXhandler = new DefaultHandler(); //create a new own handler
this.xr.setContentHandler(XMLSAXhandler);
this.xr.setErrorHandler(XMLSAXhandler);
//System.out.println("DEBUG: db_connection is " + db_connection.toString());
catch (Exception e) {
System.out.println("Constructor Error!");
e.printStackTrace();
}This time, I created a new instance of default handler() which can be referenced by as the objects's XMLSAXhandler. However, that did not solve the problem, why does the problem still persist?
Right now, there is only one instance of a default handler created. So why does all my parsing event functions still get ignored? -
I'm trying to read a XML file in an EJB using a SAX parser.
I tried the following statement to create the reader
XMLReader xr = XMLReaderFactory.createXMLReader("oracle.xml.parser.v2.SAXParser");
but I obtain the following exception:
java.lang.ClassNotFoundException: oracle.xml.parser.v2.SAXParser
at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:118)
The same works correctly from a command line application outside OC4J.
Is there another SAX Parser I can use in OC4J?
Andrea MattioliI'm also seeing the a similar problem trying to get my EJB to parse SAX using the Xerces parser. If I do
XMLReader myReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
and place xerces.jar into $(OC4J_INSTALL_ROOT)\j2ee\home\lib then at runtime I get
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
This is despite the fact that calling Class.forName() on the same class name works fine. Setting the system property org.xml.sax.driver and calling createXMLReader with no parameters results in the same error.
I can force use of the Xerces SAX parser by bypassing the factory and just calling
XMLReader myReader = new org.apache.xerces.parsers.SAXParser();
but that seems a tad hacky and I don't want to hardwire SAX parser implementation choice into my code.
Is this a known bug?
Also which parser is getting used by default in OC4J i.e. if I don't set org.xml.sax.driver and use the no parameters variant of createXMLReader()?
Thanks
Alan -
XDK for C - SAX Parser Memory Problem
Hi,
I'm using the SAX Parser with custom Callbacks with XDK for C - version : Oracle XML Parser 9.2.0.6.0.
i'm parsing a single large XML file using the custom callbacks.
The issue i'm facing that the SAX Parser does not release any memory till complete parsing is over for the whole document(until xmlterm is called). This causes the parser to fail with LPX-00002 : OUT OF MEMORY error before the document parsing is complete.
I tried using custom memory callbacks but what i noticed is that the parser does not invoke the free Memory callback untill xmlterm is invoked.
If anybody has found an alternative to allow memory to be freed while SAX parsing is going on pls help me too.
Regards,
Vineet MagoCan you send the test case?
-
How to deal with empty tags in a SAX Parser
Hi,
I hope someone can help me with the problem I am having!
Basically, I have written an xml-editor application. When an XML file is selected, I parse the file with a SAX parser and save the start and end locations of all the tags and character data. This enables me to display the xml file with the tags all nicely formatted with pretty colours. Truly it is a Joy To Behold. However, I have a problem with tags in this form:
<package name="boo"/>
because the SAX parser treats them like this:
<package name = boo>
</package>
for various complex reasons the latter is unaccetable so my question is: Is there some fiendishly clever method to detect tags of this type as they occur, so that I can treat them accordingly?
Thanks,
ChrisI have spent some time on googling for code doing this, but found nothing better, than I had to write in by myself.
So, it would be something like this. Enjoy :)
package comd;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import java.util.Stack;
import java.util.Enumeration;
public class EmptyTagsHandler extends DefaultHandler {
private StringBuilder xmlBuilder;
private Stack<XmlElement> elementStack;
private String processedXml;
private class XmlElement{
private String name;
private boolean isEmpty = true;
public XmlElement(String name) {
this.name = name;
public void setNotEmpty(){
isEmpty = false;
public EmptyTagsHandler(){
xmlBuilder = new StringBuilder();
elementStack = new Stack();
private String getElementXPath(){
StringBuilder builder = new StringBuilder();
for (Enumeration en=elementStack.elements();en.hasMoreElements();){
builder.append(en.nextElement());
builder.append("/");
return builder.toString();
public String getXml(){
return processedXml;
public void startDocument() throws SAXException {
xmlBuilder = new StringBuilder();
elementStack.clear();
processedXml = null;
public void endDocument() throws SAXException {
processedXml = xmlBuilder.toString();
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (!elementStack.empty()) {
XmlElement elem = elementStack.peek();
elem.setNotEmpty();
xmlBuilder.append("<");
xmlBuilder.append(qName);
for (int i=0; i<attributes.getLength();i++){
xmlBuilder.append(" ");
xmlBuilder.append(attributes.getQName(i));
xmlBuilder.append("=");
xmlBuilder.append(attributes.getValue(i));
xmlBuilder.append(">");
elementStack.push(new XmlElement(qName));
public void endElement(String uri, String localName, String qName) throws SAXException {
XmlElement elem = elementStack.peek();
if (elem.isEmpty) {
xmlBuilder.insert(xmlBuilder.length()-1, "/");
} else {
xmlBuilder.append("</");
xmlBuilder.append(qName);
xmlBuilder.append(">");
elementStack.pop();
public void characters(char ch[], int start, int length) throws SAXException {
if (!elementStack.empty()) {
XmlElement elem = elementStack.peek();
elem.setNotEmpty();
String str = new String(ch, start, length);
xmlBuilder.append(str);
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
String str = new String(ch, start, length);
xmlBuilder.append(str);
} -
Create document with PL/SQL xml parser
Hello,
I'm trying to create a document with PL/SQL package xmldom on 8.1.7 and write to a file.
The problem is that my file is empty when it's created.
Can anyone send me an example of this simple problem or fullfill my example so it's works.
As you understand I'm new in using XML. :)
My example is:
declare
doc xmldom.DOMDocument;
n xmldom.DOMNode;
e xmldom.domelement;
t xmldom.domtext;
begin
doc := xmldom.newdomdocument;
t := xmldom.createtextnode(doc, 'ROOT');
n := xmldom.makenode(t);
doc := xmldom.makedocument(n);
xmldom.writetofile(doc, 'd:\orant\xdk\plsql\demo\test.xml');
end;
Regards
HekanYour problem may be memory for the JavaVM. Remember, the PL/SQL
parser uses the Java XML Parser APIs which run in a VM instance.
Are you running Oracle 8i? If you are you can access our Java
XML parser loaded in 8i's VM directly from your PL/SQL code.
That is in fact how our PL/SQL Parser does it.
Finally, we have no experience loading other XML Parsers into
Oracle.
Oracle XML Team
http://technet.oracle.com
Oracle Technology Network
Premal Mehta (guest) wrote:
: Hi,
: I asked about his a few days back. Pl/SQL parser does not
work
: for XML files of size greater then 500Kb. You replied saying
: that there were no such problem.
: However whenever I try, either I get some exception or
Pl/SQL
: crashes due to memory error. I am clueless. Is there some
: setting that I can do to get away with the problem? Please
: guide...
: Also, tell me about the alternatives.
: Can I write code in Java and load these class filesin Oracle
: and then reference these classes from Pl/SQL code. Can I load
: any other parser for Java in Oracle.
: Looking forward for help...
: Premal.
null
Maybe you are looking for
-
Sum of a sum when using Top N in the Group Sort Expert
Hi All, I have a small problem I can't quite seem to work out. I have a report where each line is for a particular Product, and is a summation of the Sales for that Product over several Locations. When you click on the line, it shows the breakdown of
-
Workflow for Vendor Creation through a portal.
Hi All Workflow Experts, I have a scenario here.In my project vendor is created through a portal.On submit button from portal the workflow is to be triggered.I have to design the workflow process.So can you pleas
-
Cannot connect to internet in Solaris 10 OS.
I setup X4500 running solaris 10 OS. But, I cannot connect to internet. I do not understand whats wrong with that. When I ping google or any other site from X4500 its not transferring any packets. I can ssh it from a remote computer. So, running fine
-
Outbound queue in NOSEND status
Dear All We are doing integration between CRM2007 and lotus domino server (6.5) And we have done installation of groupware connector (2.1) and proxy (Default) and replicated all the customizing objects successfully and we are using ABAP_MAPBOX. So, w
-
Hello everyone! I am developing a graphics application using the 2D API. The UI basically has a JSplitter in the content pane that's showing a multi purpose button- and property-panel on the left side and a JPanel-derived graphics output panel. The o