Two Laptop Bag*
The moose likes XML and Related Technologies and the fly likes Removing newline character from XML input file to SAX parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Removing newline character from XML input file to SAX parser" Watch "Removing newline character from XML input file to SAX parser" New topic
Author

Removing newline character from XML input file to SAX parser

Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
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 ]
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Can someone please help me with this problem?

Whenever my datatfile (input file) has extra line in any tag value.

In that tags endElement event I see that value_clauseBuffer is null.

Any inputs from experienced developers are higly appreciated.

Thanks!
Gemini
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing newline character from XML input file to SAX parser