-- 作者:binrihui
-- 发布时间:3/16/2009 4:04:00 PM
--
/** * @(#)CreateXml.java * * @author binrihui * @version 1.00 2009/1/19 */ import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStreamWriter; import java.sql.ResultSet; import java.util.List; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.saxpath.*; public class CreateXml { private String username; private String password; private String tablename; private String filepath ="E:\\Employees2.xml"; private DB db; private ResultSet result; private boolean isconnect=false; public boolean connect() { db = new DB(databasename,username,password); isconnect=db.Isconnect(); if(isconnect){ // String sql = "select empno,ename,job,mgr,to_char(hiredate,'mm-dd-yyyy') hiredate,sal,comm,deptno from "+tablename; String sql = "select * from "+tablename; result =db.openSql(sql); db.getColumn(); } return isconnect; } public void SqlQueryResult(){ try{ while(result.next()){ System.out.println(result.getInt("EMPNO")+" "+result.getString("EName")+" "+result.getString("Job")+ " "+result.getString("MGR")+" "+result.getString("HireDate")+" "+result.getString("SAl")+ " "+result.getString("COMM")); } }catch (Exception e){ } } public void create(){ String sql = "select * from "+tablename; result =db.openSql(sql); System.out.println("result = stmt.executeQuery(sql);"); int column = db.getColumn(); System.out.println(column); String columnName[]=db.getColumnNmae(); for(int j=0;j<column;j++){ System.out.println(columnName[j]); } Element columnElement[] = new Element[column]; Document xmldocument = DocumentHelper.createDocument(); OutputFormat format =OutputFormat.createPrettyPrint(); format.setEncoding("GB2312"); Element Employees = xmldocument.addElement("Employees"); //Element bookelement =bookselement.addElement("book"); try{ //System.out.println(result.next()); while(result.next()){ Element Employee = Employees.addElement("Employee"); System.out.println("进到这"); for(int i=0;i<column;i++){ System.out.println("加一个"); columnElement[i]=Employee.addElement(columnName[i]); columnElement[i].setText(result.getString(i+1)); } } }catch(Exception e) { e.printStackTrace(); } try{ filepath = "E:\\Employees.xml"; File f = new File("E:\\Employees.xml") ; XMLWriter write =new XMLWriter(new FileWriter(f),format) ; write.write(xmldocument); write.close(); }catch(Exception e){ e.printStackTrace(); System.out.println("出错"); } } //将Xml文档数据插入到oracle中的employees2表中。 public void insertToDatabase(){ int COLUMN =db.getColumn(); String columnname[] =db.getColumnNmae(); try{ filepath = "E:\\Employees.xml"; SAXReader saxreader = new SAXReader(); Document xmldocument =saxreader.read(new File(filepath)); //System.out.print(xmldocument); Element bootelement =xmldocument.getRootElement(); List list[] =new List[COLUMN]; Iterator interator[] =new Iterator[COLUMN]; System.out.println(COLUMN); for(int i=0;i<COLUMN;i++){ //String elementpath =tablename+"s/"+tablename+"/"+columnname[i]; String elementpath ="Employees"+"/"+"Employee"+"/"+columnname[i]; //String elementpath ="Employees"+"/"+columnname[i]; //elementpath 有问题。 //String elementpath ="Employees"+"/"+columnname[i]; //System.out.println(elementpath); list[i] =xmldocument.selectNodes(elementpath); // System.out.println(list[i].isEmpty()); interator[i] =list[i].iterator(); //System.out.println(COLUMN); } System.out.println(interator[0].hasNext()); while(interator[0].hasNext()){ //System.out.println(COLUMN); Element temp[] =new Element[interator.length] ; for(int i=0;i<interator.length;i++){ temp[i]=(Element)interator[i].next(); //System.out.println(COLUMN); } // System.out.println(COLUMN); StringBuffer sqlstr =new StringBuffer("INSERT INTO "+"employees2"+" values("); sqlstr.append(temp[0].getText()) ; String ss=""; for(int j=1;j<interator.length;j++){ if(j==4){ ss=temp[j].getText().trim(); ss=ss.substring(0,10); sqlstr.append(","+"TO_DATE('"+ss+"',"+"'yyyy-mm-dd'"+")"); } else sqlstr.append(",'"+temp[j].getText()+"'"); } sqlstr.append(")"); System.out.println(sqlstr); db.insert(sqlstr.toString()); } }catch(Exception e){ e.printStackTrace(); } } public void saveXmlFile(String str){ try{ File f =new File(filepath); FileOutputStream fileoutputstream =new FileOutputStream(f); OutputStreamWriter streamwriter =new OutputStreamWriter(fileoutputstream); BufferedWriter bufferedwriter = new BufferedWriter(streamwriter); System.out.println(str); bufferedwriter.write(str); bufferedwriter.flush(); bufferedwriter.close(); streamwriter.close(); fileoutputstream.close(); }catch(Exception e){ e.printStackTrace(); } } public void showToWindow(XmlEditWindow xmledit) { try{ FileInputStream fileinputstream = new FileInputStream(new File(filepath)); int len ; byte buf[]=new byte[1024];//write.createWriter(print,"GB2312"); while((len =fileinputstream.read(buf,0,1024))!=-1){ String str =new String(buf,0,len); xmledit.addText(str); } }catch(Exception e) { e.printStackTrace() ; } } public void setFilePath(String path){ this.filepath = path; } public String getFilePath() { return filepath; } private String databasename; /** * @param databasename 要设置的 databasename。 */ public void setDatabasename(String databasename) { this.databasename = databasename; } /** * @param password 要设置的 password。 */ public void setPassword(String password) { this.password = password; } /** * @param tablename 要设置的 tablename。 */ public void setTablename(String tablename) { this.tablename = tablename; } /** * @param username 要设置的 username。 */ public void setUsername(String username) { this.username = username; } public DB getDB(){ return db; } } class Tes { public static void main(String args []){ CreateXml T= new CreateXml(); T.setDatabasename("orcl"); T.setUsername("system"); T.setPassword("oracle"); T.setTablename("EMPloyees"); T.connect(); //T.SqlQueryResult(); //为什么创建了xml文档后,就不能将xml文档导入Oracle中的employees2表。已解决 T.create(); T.insertToDatabase(); T.create(); } }
|