关闭

java生成xml文件

132人阅读 评论(0) 收藏 举报
分类:

java生成xml文件

java生成XML文件程序:
xmlPathConfig.properties
MyFilePath=d:

GenerateOne.java
import java.util.Map;
import java.util.ResourceBundle;

public class GenerateOne {
	public static String myFilePath;
	static{
		ResourceBundle resourceBundle = ResourceBundle.getBundle("xmlPathConfig");
		myFilePath = resourceBundle.getString("MyFilePath");
	}
	public boolean makeMyFile(Map<String, Object> params) {
		StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		xmlResult.append("<OUT>");
		xmlResult.append(" <DATA  CODE=\"" + params.get("code").toString() + "\"");
		xmlResult.append("  PLACENO=\"" + params.get("placeno").toString() + "\"");
		xmlResult.append("  CHECKER=\"" + params.get("checker").toString() + "\"");
		xmlResult.append("  CHECKERTIME=\"" + params.get("nowsdf").toString() + "\"");
		xmlResult.append("  MEMO=\"" + params.get("memo").toString() + "\">");
		xmlResult.append("</DATA>");
		xmlResult.append("</OUT>");

		return new GenerateXML().makeFile(xmlResult, "OUTFILE", myFilePath);
	}
}

GenerateXML.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GenerateXML {
	public static boolean Writexmlfile(StringBuilder content, String filepath, String name) {

		String filename = filepath + name + ".Dat";

		try {
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filename, true), "UTF-8");
			try {
				osw.write(content.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					osw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	
	public static Boolean WriteTextFile(StringBuilder content, String filepath, String name) {
		File file = new File(filepath + name + ".Dat");
		FileWriter fw = null;
		BufferedWriter bw = null;

		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		if (file.exists()) {
			return false;
		} else {
			try {

				fw = new FileWriter(file, false);
				bw = new BufferedWriter(fw);
				bw.write(content.toString());
				bw.flush();
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (fw != null)
						fw.close();
					if (bw != null)
						bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return true;

	}

	/**
	 * 公用生成xml文件的代码
	 *
	 * @param xmlResult
	 *            xml内容
	 * @param title
	 *            xml标题
	 * @return
	 */
	public boolean makeFile(StringBuilder xmlResult, String title, String path) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			GenerateXML.Writexmlfile(xmlResult, path.replace(" ", "").trim() + "/", title + sdf.format(new Date()));
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
}

Main.java
import java.util.HashMap;
import java.util.Map;

public class Main {

	
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello world");
		Map<String, Object> params = new HashMap<>();
		params.put("code", "code1");
		params.put("placeno", "placeno1");
		params.put("checker", "checker1");
		params.put("nowsdf", "nowsdf1"); 
		params.put("memo", "memo1"); 
		
		GenerateOne generateOne = new GenerateOne();
		generateOne.makeMyFile(params);
		
	
	}

}

 程序运行结果:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:307353次
    • 积分:7500
    • 等级:
    • 排名:第2998名
    • 原创:442篇
    • 转载:12篇
    • 译文:7篇
    • 评论:36条
    文章分类
    最新评论