问题描述
我這用JAVA生成了XML,但項目要求同時生成XSD,不知如何解決,這請教各位.貼出生成XML的代碼.packagecn.com.yyhj.ycweb.ebox.sis_test;importjava.io.FileWriter;importjava.io.IOException;importjava.sql.*;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Locale;importorg.apache.log4j.*;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.XMLWriter;importcn.com.yyhj.ycweb.ebox.util.DatabaseUtil;importcn.com.yyhj.ycweb.ebox.util.IoUtil;importcn.com.yyhj.ycweb.ebox.util.MailUtil;publicclassSender{privatestaticLoggerlogger=Logger.getLogger(Sender.class);privateParameterp=null;privateConnectionconn=null;privateStringBuffermailMsg=null;privateStringmaterShipmentcode,contShiptCode,codeshipment;privatestaticSimpleDateFormatsdf=newSimpleDateFormat("dd-MM-yyyyHH:mm:ss");privatestaticSimpleDateFormatsdf1=newSimpleDateFormat("yyyyMMddHHmmss");privatestaticSimpleDateFormatsdf2=newSimpleDateFormat("dd-MMM-yyyyHH:mm:ss",Locale.GERMAN);privatestaticSimpleDateFormatsdf3=newSimpleDateFormat("yyyyMMdd");privatestaticSimpleDateFormatsdf4=newSimpleDateFormat("dd-MMM-yyyy",Locale.GERMAN);publicstaticvoidmain(String[]args){logger.info("Start...");if(args.length>0){Sendersender=newSender(args[0]);}else{logger.error("Havenotsettheparameterfileforapp.");System.exit(1);}}/***構造方法**@paramfile*String*/publicSender(Stringfile){try{this.p=newParameter(file);java.util.Datedate=newjava.util.Date();StringOutDate=p.sdf1.format(date);conn=DatabaseUtil.createConnection(p.JDBCDriver,p.dbUrl,p.dbUser,p.dbPassword);if(this.getData(OutDate,conn)==1){logger.info("Success!");}}catch(Exceptionex){logger.error("Failed!",ex);}finally{logger.info("End!");try{if(conn!=null){conn.close();}}catch(SQLExceptione){logger.error("ClosingConnectionFailed!",e);}}}/***獲取發送的資料**@returnintifreturn0,thenhasnotdata;ifreturn1,thenhasdata;*ifreturn-1,thenoccurserror*/privateintgetData(StringeboxOutDate,Connectionconn)throwsSQLException{Statementstmt1=null;Statementstmt2=null;ResultSetheader_rs=null;ResultSetmatship_rs=null;try{stmt1=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);stmt2=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);Stringsql_header=p.sql_header;Stringsql_matship=p.sql_matship;//logger.debug("sql:"+sql_header);header_rs=stmt1.executeQuery(sql_header);matship_rs=stmt2.executeQuery(sql_matship);if(header_rs.next()){header_rs.beforeFirst();//createXmlFile(p.xml,header_rs,matship_rs,"BIG5");if(createXmlFile(p.xml,header_rs,matship_rs,"BIG5")==1){sendMail(p.smtpHost,p.smtpPort,p.from,p.to,p.dataMailSubject,p.xml,null,"big5");}else{return0;}return1;}else{logger.info("沒有需要轉出的資料");return0;}}catch(Exceptione){logger.error("創建資料發生錯誤!",e);return-1;}finally{IoUtil.deleteFile(p.xml);if(header_rs!=null){header_rs.close();}if(matship_rs!=null){matship_rs.close();}if(stmt1!=null){stmt1.close();}if(stmt2!=null){stmt2.close();}}}/***@returnboolean*@paramsmtpHost*郵件服務器地址*@paramsmtpPort*傳送端口*@paramfrom*郵件發送方地址*@paramto*郵件接收方地址*@paramsubject*郵件主旨*@paramattachment*郵件附檔*@paramcontent*郵件內容*@paramencode*郵件編碼方式*/publicstaticbooleansendMail(StringsmtpHost,intsmtpPort,Stringfrom,Stringto,Stringsubject,Stringattachment,Stringcontent,Stringencode){booleant=false;try{MailUtilmail=newMailUtil(smtpHost,smtpPort);mail.setFrom(from);//郵件發送方地址mail.setTo(to);//郵件接收方地址mail.setSubject(subject);//郵件主旨mail.setEncode(encode);//郵件編碼方式mail.setAttachment(attachment);//郵件附檔mail.setContent(content);//郵件內容mail.sendMimeMail();//發送郵件logger.info("郵件發送成功!");t=true;}catch(Exceptione){logger.error("郵件發送失敗!",e);}returnt;}/***根據傳入的數據記錄在當前目錄下集創建一個XML文件。**@paramfileName*要創建的XML文件名*@paramrs*傳入的數據記錄集*@paramencode*XML文件要使用的編碼*@throwsParseException*@throwsSQLException*@throwsParseException*/publicintcreateXmlFile(StringfileName,ResultSetheader_rs,ResultSetmatship_rs,Stringencode){try{StringsenderValue,receiverValue;Documentdocument=DocumentHelper.createDocument();ElementtableElement=document.addElement("XML");ElementrowElement1=tableElement.addElement("Header");ElementrowElement2=tableElement.addElement("MasterShipment");ResultSetMetaDataheader_rsmd=header_rs.getMetaData();intheader_columnCount=header_rsmd.getColumnCount();if(!header_rs.next())thrownewRuntimeException("Theheader_rsargumenthavenotanyrecord.");//header_rs.beforeFirst();for(inti=1;i<=header_columnCount;i++){if(i==6){rowElement1.addElement(header_rsmd.getColumnName(i)).addText(header_rs.getString(i)==null?"":sdf2.format((Date)sdf1.parse(header_rs.getString(i))));}else{rowElement1.addElement(header_rsmd.getColumnName(i)).addText(header_rs.getString(i)==null?"":header_rs.getString(i));}}if(!matship_rs.next())thrownewRuntimeException("Thersargumenthavenotanyrecord.");ResultSetMetaDatamatship_rsmd=matship_rs.getMetaData();intmatship_columncount=matship_rsmd.getColumnCount();for(intj=1;j<=matship_columncount;j++){if(j==1){materShipmentcode=matship_rs.getString(j);rowElement2.addElement(matship_rsmd.getColumnName(j)).addText(matship_rs.getString(j)==null?"":matship_rs.getString(j));}elseif(j==3){senderValue=matship_rs.getString(j);ElementsenderName=rowElement2.addElement(matship_rsmd.getColumnName(j));if(CreateAddressNode(senderName,senderValue)==1){logger.info("創建address子節點成功!");}else{logger.info("創建address子節點失敗!");return0;}}elseif(j==4){receiverValue=matship_rs.getString(j);ElementreceiverName=rowElement2.addElement(matship_rsmd.getColumnName(j));if(CreateAddressNode(receiverName,receiverValue)==1){logger.info("創建address子節點成功!");}else{logger.info("創建address子節點失敗!");return0;}}elseif(j==8||j==16||j==18){if(matship_rs.getString(j)==null||(matship_rs.getString(j)).equals("")){rowElement2.addElement(matship_rsmd.getColumnName(j)).addText("");}else{rowElement2.addElement(matship_rsmd.getColumnName(j)).addText(sdf4.format(sdf3.parse(matship_rs.getString(j))));}}else{rowElement2.addElement(matship_rsmd.getColumnName(j)).addText(matship_rs.getString(j)==null?"":matship_rs.getString(j));}}OutputFormatformat=OutputFormat.createPrettyPrint();format.setEncoding(encode);format.setTrimText(false);XMLWriterwriter=newXMLWriter(newFileWriter(fileName),format);writer.write(document);writer.close();return1;}catch(Exceptionex){logger.error("創建節點出錯!",ex);return0;}}}
解决方案
解决方案二:
哥们你的代码太长了告诉你个类JAXB,你用它来做jdk6里边有或者去下JAR
解决方案三:
謝謝!一樓的.但我看了下這個類,很多都是通過XSD生成XML,而不是生成XML是自動生成XSD.