java生成xml文件

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值