Annotation Xml 持久化

import java.io.IOException;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "jmeld")
public class JMeldSettings {
	// class variables:
	public static JMeldSettings instance;

	// Instance variables:
	@XmlElement(name = "editor")
	private String editor = "editor";
	@XmlElement(name = "filter")
	private String filter = "filter";
	@XmlElement(name = "folder")
	private String folder = "folder";

	public JMeldSettings() {
	}

	public static synchronized JMeldSettings getInstance() {
		if(instance == null) {
			synchronized(JMeldSettings.class) {
				if(instance == null) {
					instance = new JMeldSettings();
				}
			}
		}
		
		return instance;
	}
	
	/**
	 * Save a configuration to a file.
	 */
	public void save(JMeldSettings configuration) throws JAXBException, IOException {
		JaxbPersister.getInstance().save(configuration);
	}
	
	public static void main(String[] args) {
		try {
			getInstance().save(getInstance());
		} catch(JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JaxbPersister {
	// class variables:
	private static JaxbPersister instance = new JaxbPersister();

	// instance variables:
	private Map<Class, Context> contexts = new HashMap<Class, Context>();

	private JaxbPersister() {
	}

	public static JaxbPersister getInstance() {
		return instance;
	}

	/**
	 * Load a object of type 'clazz' from a file.
	 */
	public <T> T load(Class<T> clazz, File file) throws FileNotFoundException, JAXBException {
		return load(clazz, new FileInputStream(file));
	}

	/**
	 * Load a object of type 'clazz' from a file.
	 */
	public <T> T load(Class<T> clazz, InputStream is) throws JAXBException {
		Context context;
		T object;

		context = getContext(clazz);
		synchronized(context) {
			object = (T) context.unmarshal(is);
			return object;
		}
	}

	/**
	 * Save an object to a file.
	 */
	public void save(Object object) throws JAXBException, IOException {
		OutputStream os;
        File file = new File("JMeldSettings.xml");
		os = new FileOutputStream(file);
		save(object, os);
		os.close();
	}

	/**
	 * Save a object to a outputstream.
	 */
	private void save(Object object, OutputStream os) throws JAXBException, IOException {
		Writer writer;
		Context context;

		writer = new OutputStreamWriter(os);

		context = getContext(object.getClass());
		synchronized(context) {
			context.marshal(object, writer);
		}
	}

	/**
	 * Each class has it's own context to marshal and unmarshal. The context
	 * contains a jaxbcontext.
	 */
	private Context getContext(Class clazz) {
		Context c;

		synchronized(contexts) {
			c = contexts.get(clazz);
			if(c == null) {
				c = new Context(clazz);
				contexts.put(clazz, c);
			}
		}

		return c;
	}

	class Context {
		private JAXBContext jaxbContext;
		private Marshaller marshaller;
		private Unmarshaller unmarshaller;

		Context(Class clazz) {
			try {
				jaxbContext = JAXBContext.newInstance(clazz);

				marshaller = jaxbContext.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				marshaller.setSchema(null);

				unmarshaller = jaxbContext.createUnmarshaller();
				unmarshaller.setSchema(null);
			} catch(JAXBException e) {
				e.printStackTrace();
			}
		}

		public void marshal(Object object, Writer writer) throws JAXBException {
			marshaller.marshal(object, writer);
		}

		public Object unmarshal(InputStream is) throws JAXBException {
			return unmarshaller.unmarshal(is);
		}
	}
}

 

两个类,将内存中的xml信息持久化入文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值