栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

package cn.com.tiptop.outsyscheck.util;

import IceInternal.Ex;

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;


public class JaxbUtil {
	private static final Logger logger = LoggerFactory.getLogger(JaxbUtil.class);
	
	public static  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;
		}
	}
	
	
	public static  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;
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public static  T readXml(String filePath,Class 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;
	}
	
	
	@SuppressWarnings("unchecked")
	public static  T readXml(String filePath,String coding,Class 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;
	}

    
	public static void appendTask(String filePath, DBXml dbXml){
        Tasks readXml = getTasks(filePath);
        List 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 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 list = new ArrayList<>();
            List taskList = readXml.getTask();
            for (DBXml dbXml:taskList) {
                if(!dbXml.getDbUniqueId().equals(dbUniqueId)){
                    list.add(dbXml);
                }
            }
            readXml.setTask(list);
            createXml(readXml,filePath,"utf-8");
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号