该注解使用的是jdk自带的rt.jar包下的
1、javaBean注解如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="process")
public class Process extends BaseAttribute{
@XmlAttribute(name="isExecutable")
String isExecutable;
@XmlElement(name = "sequenceFlow")
List sequenceFlows;//线条集合
public Process() {
super();
}
public Process(String id,String name,String documentation,String isExecutable,
List sequenceFlows) {
super(id, name, documentation);
this.sequenceFlows = sequenceFlows;
}
public String getIsExecutable() {
return isExecutable;
}
public void setIsExecutable(String isExecutable) {
this.isExecutable = isExecutable;
}
public List getSequenceFlows() {
return sequenceFlows;
}
public void setSequenceFlows(List sequenceFlows) {
this.sequenceFlows = sequenceFlows;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class SequenceFlow extends BaseAttribute{
@XmlAttribute(name="sourceRef")
String sourceRef;//源端
@XmlAttribute(name="targetRef")
String targetRef;//目标端
@XmlElement(name = "conditionExpression")
ConditionExpression conditionExpression;
public SequenceFlow() {
super();
}
public SequenceFlow(String id, String name,String documentation,String sourceRef,
String targetRef,ConditionExpression conditionExpression) {
super(id,name,documentation);
this.sourceRef = sourceRef;
this.targetRef = targetRef;
this.conditionExpression = conditionExpression;
}
public String getSourceRef() {
return sourceRef;
}
public void setSourceRef(String sourceRef) {
this.sourceRef = sourceRef;
}
public String getTargetRef() {
return targetRef;
}
public void setTargetRef(String targetRef) {
this.targetRef = targetRef;
}
public ConditionExpression getConditionExpression() {
return conditionExpression;
}
public void setConditionExpression(ConditionExpression conditionExpression) {
this.conditionExpression = conditionExpression;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class ConditionExpression {
@XmlAttribute(name="xsi:type")
String type;
@XmlElement(name="conditionValue")
String conditionValue;
public ConditionExpression(){
}
public ConditionExpression(String type,String conditionValue) {
this.type = type;
this.conditionValue = conditionValue;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getConditionValue() {
return conditionValue;
}
public void setConditionValue(String conditionValue) {
this.conditionValue = conditionValue;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class BaseAttribute {
@XmlAttribute(name="id")
public String id;//节点id
@XmlAttribute(name="name")
public String name;//节点名称
@XmlElement(name="documentation")
public String documentation;//描述信息
public BaseAttribute(){
}
public BaseAttribute(String id,String name,String documentation) {
this.id = id;
this.name = name;
this.documentation = documentation;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDocumentation() {
return documentation;
}
public void setDocumentation(String documentation) {
this.documentation = documentation;
}
}
2、生成xml的方法,这里注意将所有需要添加的元素都加到setCDataElements数组内,如下代码所示:
@SuppressWarnings("restriction")
public class BeanToXml2 {
/**
* java对象转换为xml文件
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return xml文件的String
* @throws JAXBException
*/
@SuppressWarnings("restriction")
public static String beanToXml(Object obj,Class> load) throws Exception{
JAXBContext context = JAXBContext.newInstance(load);
OutputFormat of = new OutputFormat();
of.setCDataElements(
new String[] { "^conditionValue"});
of.setPreserveSpace(true);
of.setIndenting(true);
ByteArrayOutputStream op = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(op, of);
SAXResult result = new SAXResult(serializer.asContentHandler());
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mar.marshal(obj, result);
return op.toString("UTF-8");
}
/**
* @Title: getProcess
* @Description: TODO(流程定义部分)
* @return
* @return Process (这里用一句话描述返回结果说明)
*/
public Process getProcess(){
Process process = new Process();//2
process.setId("processId");
process.setName("名称");
process.setIsExecutable("true");
process.setSequenceFlows(getSequenceFlows());
return process;
}
public List getSequenceFlows(){
List sequenceFlows = new ArrayList();//线条集合 3.5
ConditionExpression conditionExpression1 = new ConditionExpression("tFormalExpression","${pass}");
ConditionExpression conditionExpression2 = new ConditionExpression("tFormalExpression","${!pass}");
SequenceFlow sequenceFlow1 = new SequenceFlow("flow10","同意",null,
"sourceId1","target1",conditionExpression1);
SequenceFlow sequenceFlow2 = new SequenceFlow("flow11","退回",null,
"sourceId2","target2",conditionExpression2);
sequenceFlows.add(sequenceFlow1);
sequenceFlows.add(sequenceFlow2);
return sequenceFlows;
}
public static void main(String[] args) throws Exception {
BeanToXml2 beanToXml = new BeanToXml2();
String str = BeanToXml2.beanToXml(beanToXml.getProcess(), Process.class);
System.out.println(str);
//写入到xml文件中
String xmlPath = "D:/testConfig.bpmn";
BufferedWriter bfw = new BufferedWriter(new FileWriter(new File(xmlPath)));
bfw.write(str);
bfw.close();
}
}
3、最终生成的xml如下所示: