dom4j从数据读2W条数据写入文件时内存溢出

主要是报内存溢出,我把写的操作放在循环里面的话,就只能把子节点写进去,没有了头和根节点,如果最后把整个document写进去的话,就内存溢出了,请大侠们指点指点啊,小弟在此谢过了

以下是代码:

packagehrsys.out;

importorg.dom4j.*;

importorg.dom4j.io.*;

importorg.xml.sax.SAXException;

importhrsys.db.dbcon;

importhrsys.outdata.Friends;

importjava.io.*;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Timestamp;

importjava.util.*;

publicclassFriendsViewByTableName

{

privateXMLWriterwriter=null;

//调用dom4j中的一个类来解决中文问题,在得到writer时把format传进去

OutputFormatformat=OutputFormat.createPrettyPrint();

publicvoidviewData(Adm_chgtmpadm)

{

/**指定XML编码*/

format.setEncoding("GBK");

StringtableName=null;

StringcolumnName=null;

StringcolumnValue=null;

ElementfriendElement=null;

ElementageElement=null;

Documentdocument=null;

Stringsql="";

Stringsql1="";

ResultSetrs=null;

ResultSetrs1=null;

dbconconnect=null;

StringTMPID=adm.getTMPID();

StringTMPDESC=adm.getTMPDESC();

StringALLROWS=adm.getALLROWS();

StringMARKISNEW=adm.getMARKISNEW();

StringDELETEON=adm.getDELETEON();

StringTMPDATA=adm.getTMPDATA();

StringISNEW=adm.getISNEW();

TimestampLASTUPDATE=adm.getLASTUPDATE();

Listlist=stringToken(TMPDATA);

Iteratorit=list.iterator();

try

{

document=DocumentHelper.createDocument();

ElementfriendsElement=document.addElement("DATA");

//document.setRootElement(friendsElement);

//try{

//writer.processingInstruction("xmlversion=\"1.0\"","encoding=\"GBK\"");

//}catch(SAXExceptione1){

////TODOAuto-generatedcatchblock

//e1.printStackTrace();

//}

while(it.hasNext())

{

tableName=(String)it.next();

sql="select*from"+tableName+"whereISNEW=1";

sql1="select*fromsyscolumnswhereid=object_id('"+tableName+"')";

try{

connect=newdbcon();

rs=connect.executeQuery(sql);

rs1=connect.executeQuery(sql1);

rs1.last();

introw=rs1.getRow();

Stringcolumn[]=newString[row];

rs1.beforeFirst();

System.out.println(row);

while(rs1.next())

{

for(inti=0;i<row;i++)

{

column[i]=rs1.getString("name");

rs1.next();

}

}

while(rs.next())

{

friendElement=friendsElement.addElement(tableName);

for(inti=0;i<column.length;i++)

{

columnName=column[i];

columnValue=rs.getString(columnName);

System.out.println("列名:"+columnName);

System.out.println("值:"+columnValue);

ageElement=friendElement.addElement(columnName);

ageElement.setText((columnValue+"").toString().trim());

}

//writer.write(friendElement);

//friendElement.clearContent();//释放这个Element的空间,不然会导致内存耗尽

//friendsElement.clearContent();

}

//writer.write(friendsElement);

}catch(SQLExceptione){

e.printStackTrace();

}catch(Exceptione){

e.printStackTrace();

}

finally

{

try

{

if(rs!=null)

rs.close();

if(rs1!=null)

rs1.close();

connect.closeConnect();

}

catch(SQLExceptione)

{

e.printStackTrace();

}

}

writer=newXMLWriter(newFileWriter(newFile("C:/exp/friend.xml"),true),format);

writer.write(document);

System.out.println("创建文件成功");

}

}catch(IOExceptione)

{

e.printStackTrace();

}

finally

{

if(writer!=null)

{

try

{

writer.close();

}

catch(Exceptionerr)

{

err.printStackTrace();

}

}

}

}

publicListstringToken(StringtableName)

{

Listlist=newArrayList();

StringTokenizerstok=newStringTokenizer(tableName,",");

while(stok.hasMoreElements())

{

list.add(stok.nextToken());

}

returnlist;

}

}

相关推荐