Möchte man den „zustand“ einer Java Klasse speichern so kann man diese auch als XML Datei speichern.
Dabei wird mithilfe von Annotationen an der Klasse definiert wie und in welcher Reihenfolge die Variablen gespeichert werden. Dabei kann man auch Variablen auslassen oder umbenennen, aber dazu später mehr.
Gegeben ist das wir eine Klasse Einstellung mit einer gesetzten Sprache speichern möchten.
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "sprache" }) @XmlRootElement(name = "einstellung") private static class Einstellung implements Serializable { private static final long serialVersionUID = 1l; @XmlElement(required = true) private String sprache; public Einstellung() {} public String getSprache() {return this.sprache;} public void setSprache(String inSprache) {this.sprache = inSprache;} }
Zu erkennen sind die XML Annotationen welche die Reihenfolge („propOrder“) sowie das XML RootElement darstellt.
Als erstes lesen wir eine eventuell vorhandene XML Datei.
Die Methode unmarshal des Unmarshaller Objektes liefert ein Object zurück daher muss dieses Object zur späteren Einstellung Klasse gecastet.
private static Einstellung readObject() { Einstellung einstellung = null; try { javax.xml.bind.JAXBContext jC = javax.xml.bind.JAXBContext.newInstance(Einstellung.class); javax.xml.bind.Unmarshaller unm = jC.createUnmarshaller(); einstellung = (Einstellung) unm.unmarshal(einstellungFile); } catch (javax.xml.bind.JAXBException e) { e.printStackTrace(); } return einstellung; }
Das schreiben einer XML Datei erfolgt analog zum Lesen wie folgt.
private static void writeObject(Einstellung einstellung) { try { javax.xml.bind.JAXBContext jC = javax.xml.bind.JAXBContext.newInstance(Einstellung.class); javax.xml.bind.Marshaller mars = jC.createMarshaller(); mars.marshal(einstellung, einstellungFile); } catch (javax.xml.bind.JAXBException e) { e.printStackTrace(); } }
Die fertige Klasse sieht in diesem vorliegenden Fall dementsprechend aus:
import java.io.*; import java.util.*; import java.lang.Exception; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; public class EinstellungHandler { private static final String FILENAME = "./einstellung.xml"; private static File einstellungFile; private static Einstellung einstellung; public static void main(String... args) { einstellungFile = new File(FILENAME); if (einstellungFile.exists()) { einstellung = readObject(); } else { einstellung = new Einstellung(); } einstellung.setSprache("deutsch"); writeObject(einstellung); } private static Einstellung readObject() { Einstellung einstellung = null; try { javax.xml.bind.JAXBContext jC = javax.xml.bind.JAXBContext .newInstance(Einstellung.class); javax.xml.bind.Unmarshaller unm = jC.createUnmarshaller(); einstellung = (Einstellung) unm.unmarshal(einstellungFile); } catch (javax.xml.bind.JAXBException e) { e.printStackTrace(); } return einstellung; } private static void writeObject(Einstellung einstellung) { try { javax.xml.bind.JAXBContext jC = javax.xml.bind.JAXBContext .newInstance(Einstellung.class); javax.xml.bind.Marshaller mars = jC.createMarshaller(); mars.marshal(einstellung, einstellungFile); } catch (javax.xml.bind.JAXBException e) { e.printStackTrace(); } } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "sprache" }) @XmlRootElement(name = "einstellung") private static class Einstellung implements Serializable { private static final long serialVersionUID = 1l; @XmlElement(required = true) private String sprache; public Einstellung() { } public String getSprache() { return this.sprache; } public void setSprache(String inSprache) { this.sprache = inSprache; } } }