Read process id, name attribute values with moddle after saveXML

Hi,

I have a service that saves BPMN XML to a database, however I want to get access to the process id and name attributes to store this separately along with the XML in a table. When I use moddle to read the xml definitions I get the following warnings:

could not parse node
reader.js:618 Error: id <sample-diagram> already used(…)

The definitions are also undefined.

function MyService(bpmnjs) {
   this._bpmnjs = bpmnjs;
}

module.exports = MyService;

MyService.$inject = ['bpmnjs']

MyService.prototype.save = function() {
     var self = this;

     self._bpmnjs.saveXML({ format: true }, function (err, xml) {
             if (err) {
                  console.error(err);
             } else {
                 self._bpmnjs.moddle.fromXML(xml, function (err, definitions) {
                       console.log(definitions); 

                       // I want to get the values of the process id and name here...
                 });
             }
    }
}

Is there a way to get access to these attributes without using moddle that I am unaware of?

I have tried the following moddle.fromXML(xml, 'bpmn:Process', function (err, process) {});but it just says can’t parse node and process is undefined.

Any tips much appreciated, as I might be going about it the wrong way.

Thanks

Hi @dalestone,

in your code snippet it seems, that you are importing the XML (using fromXML) although you have an existing bpmn-js instance with an already imported XML. The problem is, that moddle is not stateless and checks and caches if an id is already assigned. That is why you get the ‘id already used’ error.

If you just want to get access to the definitions you can get it as an attribute of the bpmn-js instance. In your example this would be:

self._bpmnjs.saveXML({ format: true }, function (err, xml) {
  if (err) {
    console.error(err);
  } else {
    console.log(self._bpmnjs.definitions);
  }
}

This definitions object is updated every time you change the diagram.

Hope that helps :wink:

@pedesen Thanks alot! That seems to be what I was looking for.

Example of the code:

var process = self._bpmnjs.definitions.rootElements[0];
console.log(process.id);
console.log(process.name);

1 Like

Just to add on the above, if its a collaboration the above won’t give you the process id, instead it will be a collaboration id so you might have to do something like the following:

if (process.$type === “bpmn:Collaboration”) {
// do something different, look at processRef attribute possibly
}