xml的生成,读取,加数据模板

package cn.com.tiptop.outsyscheck.util;

import IceInternal.Ex;
/*import cn.com.tiptop.common.util.ExceptionUtil;
import cn.com.tiptop.gap.sqlite.synctaskconfig.entity.DBXml;
import cn.com.tiptop.gap.sqlite.synctaskconfig.entity.Tasks;*/
import cn.com.tiptop.outsyscheck.entity.DBXml;
import cn.com.tiptop.outsyscheck.entity.Tasks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * xml生成工具
 * @author admin
 *
 */
public class JaxbUtil {
	private static final Logger logger = LoggerFactory.getLogger(JaxbUtil.class);
	/**
	 * 生成xml
	 * @param datas 数据
	 * @param filePath 文件路径
	 * @return
	 */
	public static <T> boolean createXml(T datas,String filePath) {
		try {
			File f = new File(filePath);
			if(!f.getParentFile().exists()) {
				f.getParentFile().mkdirs();
				if(!f.exists()) {
					f.createNewFile();
				}
			}
			FileWriter fw = new FileWriter(f);
			JAXBContext context = JAXBContext.newInstance(datas.getClass());
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 防止文件中文乱码
			m.marshal(datas, fw);
			fw.flush();
			fw.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 生成xml
	 * @param datas 数据
	 * @param filePath 文件路径
	 * @param coding 文件编码
	 * @return
	 */
	public static <T> boolean createXml(T datas,String filePath,String coding) {
		try {
			File f = new File(filePath);
			if(!f.getParentFile().exists()) {
				f.getParentFile().mkdirs();
			}
			FileWriter fw = new FileWriter(f);
			JAXBContext context = JAXBContext.newInstance(datas.getClass());
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.setProperty(Marshaller.JAXB_ENCODING, coding); // 防止文件中文乱码
			m.marshal(datas, fw);
			fw.flush();
			fw.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 读取xml
	 * @param filePath 文件路径
	 * @param type 接收容器
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T readXml(String filePath,Class<T> type) {
		T value = null;
		InputStream in = null;
		InputStreamReader read = null;
		try {
			JAXBContext context = JAXBContext.newInstance(type);
			Unmarshaller u = context.createUnmarshaller();
			in = new FileInputStream(filePath);
			read = new InputStreamReader(in,"UTF-8");
			value = (T) u.unmarshal(read);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(in!=null) {
					in.close();
				}
				if(read!=null) {
					read.close();
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return value;
	}
	
	/**
	 * 读取xml
	 * @param filePath 文件路径
	 * @param coding 文件编码
	 * @param type 接收容器
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T readXml(String filePath,String coding,Class<T> type) {
		T value = null;
		InputStream in = null;
		InputStreamReader read = null;
		try {
			JAXBContext context = JAXBContext.newInstance(type);
			Unmarshaller u = context.createUnmarshaller();
			in = new FileInputStream(filePath);
			read = new InputStreamReader(in,coding);
			value = (T) u.unmarshal(read);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(in!=null) {
					in.close();
				}
				if(read!=null) {
					read.close();
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return value;
	}

    /**
     * 为配置任务流量追加数据
     *
     * @param filePath
     * @param dbXml
     */
	public static void appendTask(String filePath, DBXml dbXml){
        Tasks readXml = getTasks(filePath);
        List<DBXml> list = new ArrayList<>();
        if(readXml == null){
            readXml = new Tasks();
            list.add(dbXml);
            readXml.setTask(list);
            createXml(readXml,filePath,"utf-8");
        }else {
            if(readXml.getTask()==null){
                list.add(dbXml);
            }else{
                List<DBXml> taskList = readXml.getTask();
                for (DBXml tfi:taskList) {//只保存同一个TaskUniqueID
                    if(!tfi.getDbUniqueId().equals(dbXml.getDbUniqueId())){
                        list.add(tfi);
                    }
                }
                list.add(dbXml);
            }
            readXml.setTask(list);
            createXml(readXml,filePath,"utf-8");
        }
	}

    private static Tasks getTasks(String filePath) {
        Tasks readXml = null;
        try{
            if(new File(filePath).isFile()){
                readXml = readXml(filePath, Tasks.class);
            }
        }catch (Exception e){
            logger.error("appendTask"+ ExceptionUtil.getException(e));
            e.printStackTrace();
        }
        return readXml;
    }

    /**
     *
     */
	public static void deleteTask(String filePath, String dbUniqueId){
        Tasks readXml = getTasks(filePath);
        if(readXml!=null&&readXml.getTask()!=null){
            List<DBXml> list = new ArrayList<>();
            List<DBXml> taskList = readXml.getTask();
            for (DBXml dbXml:taskList) {
                if(!dbXml.getDbUniqueId().equals(dbUniqueId)){
                    list.add(dbXml);
                }
            }
            readXml.setTask(list);
            createXml(readXml,filePath,"utf-8");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值