sax parser problem
843834Jan 20 2005 — edited Jan 20 2005hi,
i am assuming the problem is with sax parser but i cant be sure. I am parsing a xml file (about 1.4MB) with some data in it. the parser i have created reads the xml file correctly for the most part but when at some point the
"public void characters(char buf[], int offset, int len) throws SAXException"
function stops working correctly....i.e it doesnt fully read read the data between the "<start>" and "</start>" element. say it reads about 100 id's correctly---for 101 ID it does this. This is just an example. Since, the problem might be with how :
"public void characters(char buf[], int offset, int len) throws SAXException"
function is reading the data i was wondering if anybody else had encountered this problem or please let me know if i need to change something in the code: here's a part of the code :
Bascially i have created three classes to enter data into three mysql tables and as i parse the data i fill up the columns by matching the column header with the tagName.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.io.*;
import java.util.ArrayList;
import java.lang.Object;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.util.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class Echo03 extends DefaultHandler
{
StringBuffer textBuffer;
int issuedValue, prodValue;
OrdHeader header = new OrdHeader();
OrdDetail detail = new OrdDetail();
Member memInfo = new Member();
//new addition to store the dynamic value of the products
TestOrdheader prod = new TestOrdheader();
int counter;
//////////////////////////////////////////////////////////
String tag, newTag;
SetValue setVal = new SetValue();
String test;
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: cmd filename");
System.exit(1);
}
// Use an instance of ourselves as the SAX event handler
DefaultHandler handler = new Echo03();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Set up output stream
out = new OutputStreamWriter(System.out, "UTF8");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler);
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}
static private Writer out;
private String indentString = " "; // Amount to indent
private int indentLevel = 0;
//===========================================================
// SAX DocumentHandler methods
//===========================================================
public void startDocument()
throws SAXException
{
nl();
nl();
emit("START DOCUMENT");
nl();
emit("<?xml version='1.0' encoding='UTF-8'?>");
header.assign();
}
public void endDocument()
throws SAXException
{
nl(); emit("END DOCUMENT");
try {
nl();
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void startElement(String namespaceURI,
String lName, // local name
String qName, // qualified name
Attributes attrs)
throws SAXException
{
indentLevel++;
nl(); //emit("ELEMENT: ");
String eName = lName; // element name
if ("".equals(eName)) eName = qName; // namespaceAware = false
if (qName.equals("Billing")){
issuedValue = 1;
}else if (qName.equals("Shipping")){
issuedValue = 2;
}else if (qName.equals("ShippingTotal")){
issuedValue = 3;
}
//check to see if "Product" is the name of the element thats coming next
if (qName.equals("Product")){
if (issuedValue != 3){
prodValue = 1;
}
prod.addCounter();
}else{
prodValue = 0;
}
tag = eName;
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName)) aName = attrs.getQName(i);
nl();
emit(" ATTR: ");
emit(aName);
emit("\t\"");
emit(attrs.getValue(i));
emit("\"");
}
}
if (attrs.getLength() > 0) nl();
}
public void endElement(String namespaceURI,
String sName, // simple name
String qName // qualified name
)
throws SAXException
{
nl();
String eName = sName; // element name
if ("".equals(eName)){
eName = qName; // not namespaceAware
}
if ("Order".equals(eName)){
//enter into database
databaseEnter();
}
textBuffer = null;
indentLevel--;
}
public void characters(char buf[], int offset, int len)
throws SAXException
{
nl();
try {
String s = new String(buf, offset, len);
if (!s.trim().equals("")){
settag(tag, s);
s = null;
}
}catch (NullPointerException E){
System.out.println("Null pointer Exception:"+E);
}
}
//===========================================================
// Utility Methods ...
//===========================================================
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void emit(String s)
throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
// Start a new line
// and indent the next line appropriately
private void nl()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
for (int i=0; i < indentLevel; i++) out.write(indentString);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
===================================================================
///User defined methods
===================================================================
private String strsplit(String splitstr){
String delimiter = new String("=");
String[] value = splitstr.split(delimiter);
value[1] = value[1].replace(':', ' ');
return value[1];
}
public void settag(String tag, String s){
String pp_transid = null, pp_respmsg = null,pp_authid = null, pp_avs = null, pp_avszip = null;
if ((tag.equals("OrderDate")) || (tag.equals("OrderProcessingInfo"))){
if (tag.equals("OrderDate")){
StringTokenizer st = new StringTokenizer(s);
String orddate = st.nextToken();
String ordtime = st.nextToken();
header.put("ordDate", orddate);
header.put("ordTime", ordtime);
}else if (tag.equals("OrderProcessingInfo")){
StringTokenizer st1 = new StringTokenizer(s);
int tokenCount = 1;
while (tokenCount <= st1.countTokens()){
switch(tokenCount){
case 1:
String extra = st1.nextToken();
break;
case 2:
String Opp_transid = st1.nextToken();
pp_transid = strsplit(Opp_transid);
break;
case 3:
String Opp_respmsg = st1.nextToken();
pp_respmsg = strsplit(Opp_respmsg);
break;
case 4:
String Opp_authid = st1.nextToken();
pp_authid = strsplit(Opp_authid);
break;
case 5:
String Opp_avs = st1.nextToken();
pp_avs = strsplit(Opp_avs);
break;
case 6:
String Opp_avszip = st1.nextToken();
pp_avszip = strsplit(Opp_avszip);
break;
}
tokenCount++;
}
header.put("pp_transid", pp_transid);
header.put("pp_respmsg", pp_respmsg);
header.put("pp_authid", pp_authid);
header.put("pp_avs", pp_avs);
header.put("pp_avszip", pp_avszip);
}
}else{
newTag = new String(setVal.set_name(tag, issuedValue));
header.put(newTag, s);
//detail.put(newTag, s);
prod.put(newTag, s);
memInfo.put(newTag,s);
//Check to see-- if we should add this product to the database or not
boolean check = prod.checkValid(newTag, prodValue);
if (check){
prod.addValues(s);
}
setVal.clearMod();
}
}
}
==================================================================
-----------------------------------------------------------
Here's the error that i get:
java.util.NoSuchElementException
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:691)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:337)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:281)
at Echo03.main(Echo03.java:47)