Bpmn和xml文件相互转化

前端之家收集整理的这篇文章主要介绍了Bpmn和xml文件相互转化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

package com.activit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.test.Deployment;
import org.junit.Before;
import org.junit.Test;

public class BpmnTest {
ProcessEngine processEngine = null;
RepositoryService repositoryService = null;
RuntimeService runtimeService = null;
TaskService taskService = null;

/**
 * 加载配置文件
 */
@Before
public void init() {
    processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
            .buildProcessEngine();
    repositoryService = processEngine.getRepositoryService();
    runtimeService = processEngine.getRuntimeService();
    taskService = processEngine.getTaskService();
}



 @Test
 @Deployment(resources="resources/diagrams/VacationRequest.bpmn")
 public void testXmltoBpmn() throws XMLStreamException{
     ProcessDefinition processDefinition =repositoryService.createProcessDefinitionQuery().processDefinitionKey("vacationRequest").singleResult();
     //获取流程资源的名称
     String sourceName = processDefinition.getResourceName();
     //获取流程资源
     InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getId(),sourceName);
     //创建转换对象
     BpmnXMLConverter converter = new BpmnXMLConverter();
     //读取xml文件
     XMLInputFactory factory = XMLInputFactory.newInstance();
     XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
     //将xml文件转换成BpmnModel
     BpmnModel bpmnModel = converter.convertToBpmnModel((XMLStreamReader) reader);
     //验证bpmnModel是否为空
     assertNotNull(bpmnModel);
     org.activiti.bpmn.model.Process process = bpmnModel.getMainProcess();
     //验证转换的流程id
     assertEquals("leave",process.getId());                  
 }

}

还有一种更加简便的方法

xml 格式直接自动生成bpmn文件。 新建文件: bpmn20.xml文件结尾的 activiti模型。然后copy得到的xml文件,最后open with 用activiti专用的视图打开就可以了。

原文链接:https://www.f2er.com/xml/294068.html

猜你在找的XML相关文章