Java XMLWriter 快速创建xml文件

package com.proxy.util;

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

/**
 * @author: (le.qiao)
 * @e-mail: [email protected]
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a>
 * @date: 2012-8-17
 * 
 */
public class XmlWriter {

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("thirduserid", "24396353");
		map.put("thirdorderid", "2012050893537966006");

		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("singleprice", "0.1");
		map2.put("quantity", "2");

		Map<String, Map<String, String>> map3 = new HashMap<String, Map<String, String>>();
		map3.put("orderinfo", map);
		map3.put("orderdeatil", map2);

		System.out.println(XmlWriter.write(map3));
	}

	public static String write(Map<String, Map<String, String>> map) {
		String reuslt = "";
		StringBuffer sb = new StringBuffer();
		sb.append("<api_respones xmlns:damaiapi=\"http://appapi.damai.cn/1.0\">\n");
		if (map != null && map.size() > 0) {
			for (Map.Entry<String, Map<String, String>> map2 : map.entrySet()) {
				sb.append("<" + map2.getKey() + ">");
				sb.append("\n");
				for (Map.Entry<String, String> map3 : map2.getValue().entrySet()) {
					sb.append("\t<" + map3.getKey() + ">" + map3.getValue() + "</" + map3.getKey() + ">");
					sb.append("\n");
				}
				sb.append("</" + map2.getKey() + ">");
				sb.append("\n");
			}
		}

		reuslt = sb.toString();

		return reuslt;
	}
}




<api_respones xmlns:damaiapi="http://appapi.damai.cn/1.0">
<orderdeatil>
	<quantity>2</quantity>
	<singleprice>0.1</singleprice>
</orderdeatil>
<orderinfo>
	<thirdorderid>2012050893537966006</thirdorderid>
	<thirduserid>24396353</thirduserid>
</orderinfo>

你可能感兴趣的:(java,xml,File,Writer)