关闭

关于Java生成指定格式的xml文档

标签: javaxml
204人阅读 评论(0) 收藏 举报
分类:
<pre name="code" class="java">/**
*实体类
*/
package cn.voicecyber.update.entity;

public class Bug {
	private String C001; // 编号
	private int C002; // 类别
	private int C003;// 所属模块编号
	private String C004;// 所属模块名称
	private int C005;// 处理日期
	private int C006;// 重要等级
	private String C007;// 更新内容
	private String C008;// 更新内容对应ID
	private String C009;// 所属模块号对应ID
	private String C010;// 版本号

	public Bug() {
		super();
	}

	public Bug(String c001, int c002, int c003, String c004, int c005,
			int c006, String c007, String c008, String c009, String c010) {
		super();
		C001 = c001;
		C002 = c002;
		C003 = c003;
		C004 = c004;
		C005 = c005;
		C006 = c006;
		C007 = c007;
		C008 = c008;
		C009 = c009;
		C010 = c010;
	}

	public String getC001() {
		return C001;
	}

	public void setC001(String c001) {
		C001 = c001;
	}

	public int getC002() {
		return C002;
	}

	public void setC002(int c002) {
		C002 = c002;
	}

	public int getC003() {
		return C003;
	}

	public void setC003(int c003) {
		C003 = c003;
	}

	public String getC004() {
		return C004;
	}

	public void setC004(String c004) {
		C004 = c004;
	}

	public int getC005() {
		return C005;
	}

	public void setC005(int c005) {
		C005 = c005;
	}

	public int getC006() {
		return C006;
	}

	public void setC006(int c006) {
		C006 = c006;
	}

	public String getC007() {
		return C007;
	}

	public void setC007(String c007) {
		C007 = c007;
	}

	public String getC008() {
		return C008;
	}

	public void setC008(String c008) {
		C008 = c008;
	}

	public String getC009() {
		return C009;
	}

	public void setC009(String c009) {
		C009 = c009;
	}

	public String getC010() {
		return C010;
	}

	public void setC010(String c010) {
		C010 = c010;
	}

}
/**
*开始
*/
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Xml2 {
	/**
	 * 将XML文件输出到指定的路径
	 * 
	 * @param doc
	 * @param fileName
	 * @throws Exception
	 */
	public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat fomat2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String mydate = fomat2.format(date);
		String outputPath = "D:/Java/XML/UpdateInfo" + mydate + ".xml";
		generateXml(outputPath);
	}
	
	public static void generateXml(String outputPath) {
		try {
			/*********获取数据库连接*************/
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();// 数据库驱动
			String url = "jdbc:oracle:thin:@192.168.4.182:1521:PFORCL";// 数据库链接地址
			String user = "PFDEV831";// 用户名
			String password = "pfdev831";// 密码
			Connection conn = DriverManager.getConnection(url, user, password);// 建立connection
			Statement stmt = conn.createStatement();
			conn.setAutoCommit(false);// 更改jdbc事务的默认提交方式

			String sql = "select * from t_update_LIST";// 查询语句
			ResultSet rs = stmt.executeQuery(sql);// 得到结果集
			conn.commit();// 事务提交
			conn.setAutoCommit(true);// 更改jdbc事务的默认提交方式
			List<Bug> list = new ArrayList<Bug>();// 创建取结果的列表,之所以使用列表,不用数组,因为现在还不知道结果有多少,不能确定数组长度,所有先用list接收,然后转为数组
			while (rs.next()) {// 如果有数据,取出添加如list
				Bug bug = new Bug();
				bug.setC001(rs.getString(1));
				bug.setC002(rs.getInt(2));
				bug.setC003(rs.getInt(3));
				bug.setC004(rs.getString(4));
				bug.setC005(rs.getInt(5));
				bug.setC006(rs.getInt(6));
				bug.setC007(rs.getString(7));
				bug.setC008(rs.getString(8));
				bug.setC009(rs.getString(9));
				bug.setC010(rs.getString(10));
				list.add(bug);
			}
			if (list != null && list.size() > 0) {// 如果list中存入了数据,转化为数组
				Bug[] arr = new Bug[list.size()];// 创建一个和list长度一样的数组
				System.out.println(arr.length); // 看下数组长度是多少,可以省略

				for (int i = 0; i < list.size(); i++) {
					arr[i] = list.get(i);// 给数组赋值
				}
				List<Bug> list1 = Arrays.asList(arr);// 将数组转换成List
				Document doc = generateXml(list1);// 生成XML文件
				outputXml(doc, outputPath);// 将文件输出到指定的路径
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("出现异常");
		}
	}

	private static void outputXml(Document doc, String fileName)
			throws Exception {
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		DOMSource source = new DOMSource(doc);
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 设置文档的换行与缩进
		PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
		StreamResult result = new StreamResult(pw);
		transformer.transform(source, result);
		System.out.println("生成XML文件成功!");
	}

	/**
	 * 生成XML文件
	 * 
	 * @param list
	 * @return
	 */
	public static Document generateXml(List<Bug> list) {
		Document doc = null;
		Element root = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.newDocument();
			root = doc.createElement("Modules");
			doc.appendChild(root);
		} catch (Exception e) {
			e.printStackTrace();
			return null;// 如果出现异常,则不再往下执行
		}

		int len = list.size();
		Element element;
		for (int i = 0; i < len; i++) { // 打印的顺序是根据英文字母先后顺序排序的
			Bug Bug = list.get(i);
			element = doc.createElement("Module");
			element.setAttribute("SerialNo", "" + Bug.getC001());
			element.setAttribute("Name", "" + Bug.getC001());
			element.setAttribute("Type", "" + Bug.getC002());
			element.setAttribute("ModuleID", "" + Bug.getC003());
			element.setAttribute("ModuleName", "" + Bug.getC004());
			//element.setAttribute("ErrorReply", "" + Bug.getC005());
			element.setAttribute("Levels", "" + Bug.getC006());
			element.setAttribute("Content", "" + Bug.getC007());
			element.setAttribute("Lang", "" + Bug.getC008());
			element.setAttribute("ModuleLangID", "" + Bug.getC009());
			element.setAttribute("Description", "" + Bug.getC010());
			root.appendChild(element);
		}
		return doc;
	}
}
源码下载:xml
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27295次
    • 积分:565
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:1篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论