package com.cim.idm.mwmsextend.generic.util;

import java.io.ByteArrayOutputStream;
import java.util.List;

import com.cim.idm.wmsextend.generic.util.SMessageUtil;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XmlUtil {
	public Document clone(Document doc){
		return (Document) doc.clone();
	}
	
	public void setHeaderItemValue(Document doc,String itemName, String itemValue){
		SMessageUtil.setItemValue(doc, SMessageUtil.Header_Tag, itemName, itemValue);
	}
	
	public void setElement(Document doc, Element bodyElement){
		doc.setContent(bodyElement);
	}
	
	public void printElement(Element element){
		
	}
	
	public void printElement(List list) throws Exception{
		for(Object obj : list){
			if(obj instanceof Element){
				Element elm = (Element)obj;
				elm.detach();
				Document doc = new Document(elm);
				String docstring = doc2String(doc);
				System.out.println(docstring);
			}
		}
			
	}
	
	public static String doc2String(Document doc) throws Exception {     
		Format format = Format.getPrettyFormat();     
		format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8，解决中文问题     
		XMLOutputter xmlout = new XMLOutputter(format);     
		ByteArrayOutputStream bo = new ByteArrayOutputStream();     
		xmlout.output(doc, bo);     
		return bo.toString();     
		}

}
