Use XML as a variable


I am using the modeler as a base tool and adding my own functionalities to it. Using the standard modeler.saveXML function, it’s already possible to download the XML model (which uses the xml variable). However, I am trying to convert the XML model to JSON before downloading it, so I need the XML file/text as a variable. How can I achieve this?

The xml variable gets its value with var xml =; and I have tried to make that a global variable so I can use it elsewhere in my code, but that doesn’t work.

Kind regards

Please provide more context, i.e. a longer code snippet that show how you intend to use the library.

I have found something that kind of solves my problem, although it might not be the most ‘beautiful’ solution. For now, I have updated the SaveDiagram function in the Modeler example to the following:

saveDiagram(function(err, xml) {
      setEncoded(downloadLink, 'diagram.bpmn', err ? null : xml);
      updatedXML = xml;

updatedXML is a global variable which I can now use to convert to JSON. It only works once the initial model that is loaded has changed at least once, but that’s fine.

I also found that using bpmn-js-cli I could log the XML to the console, by using'bpmn'); but I didn’t know how to use that XML model after that.

What is the use-case of converting the XML to JSON? What are you trying to achieve and how is the JSON supposed to look like? What information is it going to contain?

I am connecting my bpmn-js modeler to a service that analyses model elements. That service communicates using JSON files, so I need to convert the XML elements (element id’s, labels, sequence flows, etc) to be contained in a JSON file. So that file basically contains a list of elements with their id’s and labels.

In that case you are better off inspecting the loaded model:

var bpmnJS = ...;

bpmnJS.importXML(someXML, function(err) {
  var definitions = bpmnJS.getDefinitions();

  // definitions = { rootElements, ... }