Java XML Writer 빠 른 xml 파일 생 성

2559 단어 자바xmlFileWriter

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\">
"); if (map != null && map.size() > 0) { for (Map.Entry<String, Map<String, String>> map2 : map.entrySet()) { sb.append("<" + map2.getKey() + ">"); sb.append("
"); for (Map.Entry<String, String> map3 : map2.getValue().entrySet()) { sb.append("\t<" + map3.getKey() + ">" + map3.getValue() + "</" + map3.getKey() + ">"); sb.append("
"); } sb.append("</" + map2.getKey() + ">"); sb.append("
"); } } 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>

좋은 웹페이지 즐겨찾기