JXmlUtils.java 3.31 KB
package com.thinkgem.jeesite.common.utils.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.thinkgem.jeesite.common.config.Global;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class JXmlUtils {
	
	public static final String messageBStr = "<Message>\n";
	public static final String dataBStr = "<Data>\n";
	public static final String dataEStr = "\n</Data>";
	public static final String messageEStr = "\n</Message>";

	/***
	* @Title: strChangeXML 
	* @Description: 调用生成XML报文文件的函数,参数为生成文件名和要生成的内容字符串
	* @param @param filename
	* @param @param str
	* @param @throws IOException    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void strChangeXML(String filename, String str) throws IOException {
		SAXReader saxReader = new SAXReader();
		Document document;
		File file =new File(Global.getUserfilesBaseDir() + Global.UP_DATA_URL);
		if(!file.exists() && !file.isDirectory()){       
		    file.mkdir();    
		}
		String filenameStr = Global.getUserfilesBaseDir() + Global.UP_DATA_URL+"/"+filename+".xml";
		try {
			document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
			OutputFormat outFmt = OutputFormat.createPrettyPrint();
			outFmt.setNewLineAfterDeclaration(false);
			outFmt.setIndent(true);
			outFmt.setIndent("	");
		    outFmt.setEncoding("UTF-8");
		    XMLWriter output = new XMLWriter(new FileOutputStream(filenameStr), outFmt);
		    output.write(document);
		    output.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	
	   /**
     * 方法名:XML2Bean
     * 描述:XML转换为对象
     * 作者:倪军军
     * 日期:2015-6-23 下午12:59:51
     * @param input
     * @param clazz
     * @param alias
     * @return
     * @throws IOException
     * @return T
     */
    @SuppressWarnings("unchecked")
    public static <T> T XML2Bean(ByteArrayInputStream input, Class<T> clazz, Map<String,Class> alias) throws IOException {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(clazz);
        if(alias != null){
            for(Map.Entry<String, Class> entry:alias.entrySet()){
                xstream.alias(entry.getKey(),entry.getValue());
            } 
        }
        return (T) xstream.fromXML(input);
    }
    
    /**
     * 方法名:getResponse
     * 描述:获取响应XML
     * 作者:倪军军
     * 日期:2015-6-23 上午11:49:49
     * @param body
     * @param code
     * @param msg
     * @return
     * @return String
     */
    public static String getResponse(Object body,String code,String msg){
        // 对象序列化  
        XStream xstream = new XStream(new DomDriver("UTF-8"));  
        xstream.autodetectAnnotations(true);  
//        xstream.setMode(XStream.NO_REFERENCES);
        /*
        Response response = new Response();
        response.setMsg_code(code);
        response.setMsg(msg);
        response.setBody(body);
        return xstream.toXML(response);
        */
        return "";
    }
}