本文實(shí)例講述了java生成XML的方法。分享給大家供大家參考,具體如下:
下拉框的生成,我是通過javascript讀取xml文件生成的。Xml文件是根據(jù)數(shù)據(jù)庫生成的。Xml文件只相當(dāng)于頁面到數(shù)據(jù)庫的一道緩存。這樣利于性能。生成xml文件又是一件繁瑣的事情。只好交給機(jī)器去做了。真正的情景是程序定期自動或人為手動觸發(fā)程序生成xml。今天我單獨(dú)把xml文件生成的功能剝離出來寫了一個(gè)小程序。
具體的實(shí)現(xiàn)是,使用jxl.jar讀?。ㄎ页姓J(rèn)我很喜歡使用Execel寫配置)的SQL語句。SQL要指明哪些是名稱、哪些是代碼、哪些是父級代碼。Mybatis查詢數(shù)據(jù),拼裝報(bào)文寫入文件。這次寫了一個(gè)jar包程序。運(yùn)行前請自備jre。
核心代碼:XmlCreateService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.fitweber.service; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.fitweber.util.CommonUtils; import com.fitweber.util.ExecelUtils; /** * <pre> * XML文件生成器 * </pre> * @author wheatmark [email protected] * @version 1.00.00 * <pre> * 修改記錄 * 修改后版本: 修改人: 修改日期: 修改內(nèi)容: * </pre> */ public class XmlCreateService { @SuppressWarnings ({ "rawtypes" , "unused" , "unchecked" }) public static void main(String[] argc){ String resource = "META-INF/conf/mybatis-config.xml" ; String root = "" ; InputStream inputStream; try { //拿到數(shù)據(jù)庫連接 inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); //拿到查詢參數(shù) List requestList = ExecelUtils.readExecelSimple( "xmlmaker.xls" ); //定義變量 int i,j,listSize; String filename,sqlstament,temp;; HashMap requestMap = new HashMap(); Map map; StringBuffer buf = new StringBuffer(); for (Object l:requestList){ List list = (List)l; listSize = list.size(); filename =(String)list.get( 1 ); sqlstament =(String)list.get( 2 ); requestMap.put( "sql" , sqlstament); List result = session.selectList( "com.fitweber.dao.XmlCreateDao.xmlDataQuery" ,requestMap); for (Object r:result){ buf.append( "<option>" ); map=(Map)r; temp = (String) map.get( "DM" ); if (temp!= null ){ buf.append( "<dm>" +temp+ "</dm>" ); } temp = (String) map.get( "MC" ); if (temp!= null ){ buf.append( "<mc>" +temp+ "</mc>" ); } temp = (String) map.get( "PC" ); if (temp!= null ){ buf.append( "<pc>" +temp+ "</pc>" ); } temp = (String) map.get( "ITEM" ); if (temp!= null ){ buf.append( "<item>" +temp+ "</item>" ); } buf.append( "</option>" ); } CommonUtils.saveFile( null , (System.getProperty( "user.dir" )+ "\\xml\\" ).replace( "\\" , "/" )+filename, ( "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><select>" +buf.toString()+ "</select></root>" ), false ); buf.setLength( 0 ); } session.close(); } catch (IOException e) { e.printStackTrace(); } } } |
完整的源碼在github維護(hù),地址:https://github.com/ladykiller/xmlmaker。
完整實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對大家java程序設(shè)計(jì)有所幫助。