I have written SAX parser in
Java to parse an XML file.
<root>
<dept>
<id>111</id>
<name>aaa</name>
<description>this is for all accounts purpose. only for south zone
does not take care of consolidated accounts.
</description>
</dept>
<dept>
<id>222</id>
<name>ppp</name>
<description>this is for all HR purpose. only for south zone
does not take care of consolidated accounts.
</description>
</dept>
</root>
By parsing this file I am creating Insert statments to insert this data to Oracle.
First row and rows similar to that are getting parsed and inserted with no problem.
However when datat file has got additional newline as seen in record two above I am getting an error-
java.lang.StringIndexOutOfBoundsException:
String index out of range: -7
When I remove this additional line from the datat file manually, the program works fine.
How do I achieve this programatically.
Here is
public void characters(char[] ch, int start, int length)
throws SAXException {
if (thisElement !="root"){
if ((length == 0) && (thisElement !="") ){
table_name = thisElement;
sql = " Insert into "+ table_name +"(";
value_clause="";
value_clauseBuffer =null;
columnNames = "";
}
if ((length != 0) && (thisElement!="") && (thisElement!=table_name)){
String s = new String(ch, start, length);
String newString = s.replaceAll("'", "''");
/* This is where I was thinking I need some way of removing newline character */ if (value_clauseBuffer== null){
value_clauseBuffer = new StringBuffer(s);
}
else{
value_clauseBuffer.append(s);
}
}
}
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (thisElement !="root"){
if (!(value_clauseBuffer == null)) {
value_clauseBuffer.append("', '");
}
if (qName == table_name){
if (!(value_clauseBuffer == null)){
value_clause = "'"+value_clauseBuffer;
}
value_clause=value_clause.substring(0, (value_clause.length()-7));
columnNames =columnNames.substring(1, columnNames.length());
sql = sql + columnNames + " ) values (" + value_clause + "); ";
rest of the code to pass sql to
JDBC....
Please help!
Thanks!
Gemini
[ May 12, 2005: Message edited by: Gemini Moses ]