bpmn-moddle

repository·main·Indexed 19 days ago

https://github.com/bpmn-io/bpmn-moddle

A moddle wrapper for BPMN 2.0 used for reading and writing BPMN 2.0 diagram files in NodeJS and browser environments. It provides the BpmnModdle class to parse XML strings into a JavaScript object model via fromXML(), create new elements via create(), and serialize models back to XML via toXML(), ensuring compliance with the BPMN 2.0 meta-model.

Tokens
885
Snippets
3
Records
4
Agent score
18%

What's inside bpmn-moddle

  1. Read and write BPMN 2.0 XML with BpmnModdle

    main

    The BpmnModdle class allows you to parse BPMN 2.0 XML strings into a JavaScript object model and convert that model back into valid BPMN 2.0 XML. It uses the official BPMN 2.0 meta-model to ensure validation and correctness.

    Key workflows:

    1. Parsing: Use moddle.fromXML(xmlString) to get the root element.
    2. Manipulation: Use .set(key, value) to update attributes and .get(key) to retrieve elements. Use moddle.create(type, properties) to instantiate new BPMN elements.
    3. Serialization: Use moddle.toXML(rootElement) to generate the updated XML string.
    import { BpmnModdle } from 'bpmn-moddle';
    
    const moddle = new BpmnModdle();
    
    const xmlStr =
      '<?xml version="1.0" encoding="UTF-8"?>' +
      '<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" ' +
                         'id="empty-definitions" ' +
                         'targetNamespace="http://bpmn.io/schema/bpmn">' +
      '</bpmn2:definitions>';
    
    // Parse XML
    const {
      rootElement: definitions
    } = await moddle.fromXML(xmlStr);
    
    // Update an attribute
    definitions.set('id', 'NEW ID');
    
    // Create and add a new element
    const bpmnProcess = moddle.create('bpmn:Process', { id: 'MyProcess_1' });
    definitions.get('rootElements').push(bpmnProcess);
    
    // Convert back to XML
    const {
      xml: xmlStrUpdated
    } = await moddle.toXML(definitions);
  2. Use the BpmnModdle class

    main

    The BpmnModdle class is the primary entry point for the library. It provides methods for XML parsing and serialization based on the BPMN 2.0 meta-model.

    • fromXML(xml): Asynchronously parses an XML string. Returns an object containing the rootElement.
    • toXML(element): Asynchronously serializes a BPMN element back to an XML string. Returns an object containing the xml string.
    • create(type, properties): Creates a new BPMN element of the specified type with the provided properties.
  3. Use the BpmnModdle class to parse and write BPMN 2.0 XML

    main

    The BpmnModdle class is the primary entrypoint for the library. It provides the functionality required to parse BPMN 2.0 XML into a JavaScript object model and to write a JavaScript object model back into BPMN 2.0 XML. It is exported as the default export from the package.

    import BpmnModdle from 'bpmn-moddle';
    
    const moddle = new BpmnModdle();
    
    // Example usage (conceptual):
    // const modeling = await moddle.fromXML(xmlString);
    // const { xml } = await moddle.toXML(modeling);