Java XML 序列化和反序列化
Utils 类:
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.FileWriter; 4 import java.io.InputStreamReader; 5 import java.io.Serializable; 6 import java.io.UnsupportedEncodingException; 7 8 import javax.xml.bind.JAXBContext; 9 import javax.xml.bind.JAXBException; 10 import javax.xml.bind.Marshaller; 11 import javax.xml.bind.Unmarshaller; 12 13 /** 14 * Created by Leo on 2017/2/10. 15 */ 16 public class XmlUtils { 17 @SuppressWarnings({ "unchecked", "rawtypes" }) 18 public static <T extends Serializable> T deserialize(String xmlFilePath, Class clazz) 19 throws FileNotFoundException, JAXBException, UnsupportedEncodingException { 20 JAXBContext context = JAXBContext.newInstance(clazz); 21 Unmarshaller unmarshal = context.createUnmarshaller(); 22 FileInputStream fis = new FileInputStream(xmlFilePath); 23 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 24 return (T) unmarshal.unmarshal(isr); 25 } 26 27 public static <T> void serialize(T obj, String xmlFilePath) { 28 FileWriter writer = null; 29 try { 30 JAXBContext context = JAXBContext.newInstance(obj.getClass()); 31 Marshaller marshal = context.createMarshaller(); 32 marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 33 marshal.marshal(obj, System.out); 34 writer = new FileWriter(xmlFilePath); 35 marshal.marshal(obj, writer); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 } 40 41 }
根目录对应的类:
1 import javax.xml.bind.annotation.XmlElement; 2 import javax.xml.bind.annotation.XmlRootElement; 3 import java.io.Serializable; 4 import java.util.*; 5 6 /** 7 * Created by Leo on 2017/2/10. 8 */ 9 @XmlRootElement(name = "WorkflowProcesses") 10 public class WorkflowProcessList implements Serializable { 11 12 @XmlElement(name = "WorkflowProcess") 13 private List<WorkflowProcess> items; 14 15 public WorkflowProcessList() { 16 this.items = new ArrayList<WorkflowProcess>(); 17 } 18 19 public void addProcess(WorkflowProcess process) { 20 this.items.add(process); 21 } 22 23 public Iterator<WorkflowProcess> iterator() { 24 return items.iterator(); 25 } 26 27 public List<WorkflowProcess> getItems() { 28 return this.items; 29 } 30 31 }
子类:
1 import java.io.Serializable; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import javax.xml.bind.annotation.XmlAccessType; 6 import javax.xml.bind.annotation.XmlAccessorType; 7 import javax.xml.bind.annotation.XmlAttribute; 8 import javax.xml.bind.annotation.XmlElement; 9 import javax.xml.bind.annotation.XmlElementWrapper; 10 import javax.xml.bind.annotation.XmlRootElement; 11 import javax.xml.bind.annotation.XmlType; 12 13 @XmlType(propOrder = { "id", "name", "activities", "transitions" }) 14 @XmlAccessorType(XmlAccessType.FIELD) 15 @XmlRootElement(name = "wp") 16 public class WorkflowProcess implements Serializable { 17 18 @XmlAttribute(name = "Id") 19 private String id; 20 21 @XmlAttribute(name = "Name") 22 private String name; 23 24 @XmlElementWrapper(name = "Activities") 25 @XmlElement(name = "Activity") 26 private List<Activity> activities; 27 28 @XmlElementWrapper(name = "Transitions") 29 @XmlElement(name = "Transition") 30 private List<Transition> transitions; 31 32 public WorkflowProcess() { 33 this.activities = new ArrayList<Activity>(); 34 this.transitions = new ArrayList<Transition>(); 35 } 36 37 public String getName() { 38 return this.name; 39 } 40 41 public void setName(String value) { 42 this.name = value; 43 } 44 45 public String getId() { 46 return this.id; 47 } 48 49 public void setId(String value) { 50 this.id = value; 51 } 52 53 public void addActivity(Activity activity) { 54 this.activities.add(activity); 55 } 56 57 public List<Activity> getActivities() { 58 return this.activities; 59 } 60 61 public List<Transition> getTransitions() { 62 return this.transitions; 63 } 64 65 }
子类2:
1 import java.io.Serializable; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 import javax.xml.bind.annotation.XmlAttribute; 6 import javax.xml.bind.annotation.XmlElement; 7 import javax.xml.bind.annotation.XmlRootElement; 8 import javax.xml.bind.annotation.XmlType; 9 10 /** 11 * Created by Leo on 2017/2/10. 12 */ 13 @XmlType(name = "Activity", propOrder = { "id", "type", "name", "width", "height", "coordinate" }) 14 @XmlAccessorType(XmlAccessType.FIELD) 15 @XmlRootElement 16 public class Activity implements Serializable { 17 18 @XmlAttribute(name = "Id") 19 private String id; 20 21 @XmlAttribute(name = "Type") 22 private String type; 23 24 @XmlAttribute(name = "Name") 25 private String name; 26 27 @XmlAttribute(name = "Width") 28 private int width; 29 30 @XmlAttribute(name = "Height") 31 private int height; 32 33 @XmlElement(name = "Coordinates") 34 private Coordinate coordinate; 35 36 public Activity() { 37 38 } 39 40 public Activity(String id, String type, String name) { 41 this.id = id; 42 this.type = type; 43 this.name = name; 44 } 45 46 public String getId() { 47 return this.id; 48 } 49 50 public void setId(String value) { 51 this.id = value; 52 } 53 54 public String getType() { 55 return this.type; 56 } 57 58 public void setType(String value) { 59 this.type = value; 60 } 61 62 public String getName() { 63 return this.name; 64 } 65 66 public void setName(String value) { 67 this.name = value; 68 } 69 70 public Coordinate getCoordinate() { 71 return this.coordinate; 72 } 73 74 public void setCoordinate(Coordinate value) { 75 this.coordinate = value; 76 } 77 78 public int getWidth() { 79 return this.width; 80 } 81 82 public void setWidth(int value) { 83 this.width = value; 84 } 85 86 public int getHeight() { 87 return this.height; 88 } 89 90 public void setHeight(int value) { 91 this.height = value; 92 } 93 94 }
子类3:
1 import javax.xml.bind.annotation.XmlAccessType; 2 import javax.xml.bind.annotation.XmlAccessorType; 3 import javax.xml.bind.annotation.XmlAttribute; 4 import javax.xml.bind.annotation.XmlElement; 5 import javax.xml.bind.annotation.XmlRootElement; 6 import javax.xml.bind.annotation.XmlType; 7 8 /** 9 * Created by Leo on 2017/2/10. 10 */ 11 @XmlType(name = "Transition", propOrder = { "id", "from", "to", "name", "coordinate" }) 12 @XmlAccessorType(XmlAccessType.FIELD) 13 @XmlRootElement() 14 public class Transition { 15 16 @XmlAttribute(name = "Id") 17 private String id; 18 19 @XmlAttribute(name = "From") 20 private String from; 21 22 @XmlAttribute(name = "To") 23 private String to; 24 25 @XmlAttribute(name = "Name") 26 private String name; 27 28 @XmlElement(name = "Coordinates") 29 private Coordinate coordinate; 30 31 public String getId() { 32 return this.id; 33 } 34 35 public void setId(String value) { 36 this.id = value; 37 } 38 39 public String getFrom() { 40 return this.from; 41 } 42 43 public void setFrom(String value) { 44 this.from = value; 45 } 46 47 public String getTo() { 48 return this.to; 49 } 50 51 public void setTo(String value) { 52 this.to = value; 53 } 54 55 public String getName() { 56 return this.name; 57 } 58 59 public void setName(String value) { 60 this.name = value; 61 } 62 63 public Coordinate getCoordinate() { 64 return this.coordinate; 65 } 66 67 public void setCoordinate(Coordinate value) { 68 this.coordinate = value; 69 } 70 71 }