Utils 类:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; /**
* Created by Leo on 2017/2/10.
*/
public class XmlUtils {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T extends Serializable> T deserialize(String xmlFilePath, Class clazz)
throws FileNotFoundException, JAXBException, UnsupportedEncodingException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshal = context.createUnmarshaller();
FileInputStream fis = new FileInputStream(xmlFilePath);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
return (T) unmarshal.unmarshal(isr);
} public static <T> void serialize(T obj, String xmlFilePath) {
FileWriter writer = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshal.marshal(obj, System.out);
writer = new FileWriter(xmlFilePath);
marshal.marshal(obj, writer);
} catch (Exception e) {
e.printStackTrace();
}
} }
根目录对应的类:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.*; /**
* Created by Leo on 2017/2/10.
*/
@XmlRootElement(name = "WorkflowProcesses")
public class WorkflowProcessList implements Serializable { @XmlElement(name = "WorkflowProcess")
private List<WorkflowProcess> items; public WorkflowProcessList() {
this.items = new ArrayList<WorkflowProcess>();
} public void addProcess(WorkflowProcess process) {
this.items.add(process);
} public Iterator<WorkflowProcess> iterator() {
return items.iterator();
} public List<WorkflowProcess> getItems() {
return this.items;
} }
子类:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; @XmlType(propOrder = { "id", "name", "activities", "transitions" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "wp")
public class WorkflowProcess implements Serializable { @XmlAttribute(name = "Id")
private String id; @XmlAttribute(name = "Name")
private String name; @XmlElementWrapper(name = "Activities")
@XmlElement(name = "Activity")
private List<Activity> activities; @XmlElementWrapper(name = "Transitions")
@XmlElement(name = "Transition")
private List<Transition> transitions; public WorkflowProcess() {
this.activities = new ArrayList<Activity>();
this.transitions = new ArrayList<Transition>();
} public String getName() {
return this.name;
} public void setName(String value) {
this.name = value;
} public String getId() {
return this.id;
} public void setId(String value) {
this.id = value;
} public void addActivity(Activity activity) {
this.activities.add(activity);
} public List<Activity> getActivities() {
return this.activities;
} public List<Transition> getTransitions() {
return this.transitions;
} }
子类2:
import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; /**
* Created by Leo on 2017/2/10.
*/
@XmlType(name = "Activity", propOrder = { "id", "type", "name", "width", "height", "coordinate" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Activity implements Serializable { @XmlAttribute(name = "Id")
private String id; @XmlAttribute(name = "Type")
private String type; @XmlAttribute(name = "Name")
private String name; @XmlAttribute(name = "Width")
private int width; @XmlAttribute(name = "Height")
private int height; @XmlElement(name = "Coordinates")
private Coordinate coordinate; public Activity() { } public Activity(String id, String type, String name) {
this.id = id;
this.type = type;
this.name = name;
} public String getId() {
return this.id;
} public void setId(String value) {
this.id = value;
} public String getType() {
return this.type;
} public void setType(String value) {
this.type = value;
} public String getName() {
return this.name;
} public void setName(String value) {
this.name = value;
} public Coordinate getCoordinate() {
return this.coordinate;
} public void setCoordinate(Coordinate value) {
this.coordinate = value;
} public int getWidth() {
return this.width;
} public void setWidth(int value) {
this.width = value;
} public int getHeight() {
return this.height;
} public void setHeight(int value) {
this.height = value;
} }
子类3:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; /**
* Created by Leo on 2017/2/10.
*/
@XmlType(name = "Transition", propOrder = { "id", "from", "to", "name", "coordinate" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement()
public class Transition { @XmlAttribute(name = "Id")
private String id; @XmlAttribute(name = "From")
private String from; @XmlAttribute(name = "To")
private String to; @XmlAttribute(name = "Name")
private String name; @XmlElement(name = "Coordinates")
private Coordinate coordinate; public String getId() {
return this.id;
} public void setId(String value) {
this.id = value;
} public String getFrom() {
return this.from;
} public void setFrom(String value) {
this.from = value;
} public String getTo() {
return this.to;
} public void setTo(String value) {
this.to = value;
} public String getName() {
return this.name;
} public void setName(String value) {
this.name = value;
} public Coordinate getCoordinate() {
return this.coordinate;
} public void setCoordinate(Coordinate value) {
this.coordinate = value;
} }