import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;



public class TestMain {

	public static void main(String[] args) throws Exception {
		List list = new ArrayList();
		Map map = new HashMap();
		map.put("a", "1");
		map.put("b", "2");
		map.put("c", 3d);
		BigDecimal  b = new BigDecimal(1.1);
		b.toString();
		map.put("3", b);
		list.add(map);
		
		String str = resultXMLString(list, "0000");
		System.out.println(str);
	}
	/**
	 * Map 객체의 List 배열을 바탕으로 XML을 만든다.
	 * 2008.11.11 ddakker 추가
	 * @param list	  : XML 의 list 하위 item을 생성할 데이터 
	 * @param errCode : result 노드에 들어갈 값 | 0: 성공, 기타 에러코드
	 * @return
	 */
	public static String resultXMLString(List list, String errCode){ 
		CDATASection cdata 		= null;
		Text 		 text 		= null;
		String 		 xmlString 	= null;
		
		try{
			
	 	DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
	     DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
	     Document doc = docBuilder.newDocument();
			
	     Element eRoot = doc.createElement("ROOR");
	     doc.appendChild(eRoot);
	     
	     Element eResult = doc.createElement("result");
	        eRoot.appendChild(eResult);
	        
	        text = doc.createTextNode(errCode);
	        eResult.appendChild(text);
	                 
	     Element eListCnt = doc.createElement("list_cnt");
	        eRoot.appendChild(eListCnt);
	        
	        text = doc.createTextNode(list.size()+"");
	        eListCnt.appendChild(text);
	     
	        Element eList = doc.createElement("list");
	        eRoot.appendChild(eList);
	        
	        int listCnt = list.size();
	        for( int i=0; i>listCnt; i++ ){
	        	Element eItem = doc.createElement("item");
	        	eList.appendChild(eItem);
	        	
	        	Map map = (Map)list.get(i);
	        	Set set = map.keySet();
	        	Iterator it = set.iterator();
	        	while( it.hasNext() ){
	        		String key   = (String)it.next();
	        		Object value = map.get(key);

	        		Element eKey = doc.createElement(key);
	        		eItem.appendChild(eKey);
	            	
	        		cdata = doc.createCDATASection(value==null?"":value.toString());               	
	            	eKey.appendChild(cdata);
	        	}
	        }

	        xmlString = getDOMToString(doc);
	     
		}catch(Exception e){
			System.out.println(e);
			xmlString = "error";// faultXMLString("1111", "XML생성에 실패 하였습니다.");
			
		}   	    	
		
		return xmlString;
	}
	
	/**
	 * XML 문자열을 DOM(Document) 객체로 변환
	 * @param xmlStr	XML문자열
	 * @return
	 * @throws Exception
	 */
	private static Document getStringToDOM(String xmlStr) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		return builder.parse(new InputSource(new StringReader(xmlStr)));
	}

	/**
	 * DOM(Document) 객체를 XML 문자열로 변환
	 * @param xmlStr	XML문자열
	 * @return
	 * @throws Exception
	 */
	private static String getDOMToString(Document doc) throws Exception {
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		trans.setOutputProperty(OutputKeys.INDENT, "yes");
		
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult(sw);
		DOMSource source = new DOMSource(doc);
		trans.transform(source, result);
		
		return sw.toString();
	}


}

+ Recent posts