Adding custom attributes

Hey guys,

is it possbile to add several custom attributes to the bpmn via Java?
Using the fucnton below, leads to the erros:
DOM document is not valid… Caused by: org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: Attribute ‘customclass’ is not allowed to appear in element ‘userTask’.

protected T createElement(BpmnModelElementInstance parentElement, String id, Class elementClass) {
_ T element = modelInstance.newInstance(elementClass);_
_ element.setAttributeValue(“id”, id, true);_
_ parentElement.addChildElement(element);_

_ element.setAttributeValue(“customclass”, “name”, true);_
_ parentElement.addChildElement(element);_
_ return element;_
_ }_

Thanks John

Hi John,

you have to use a custom namespace for your attribute. Otherwise, your BPMN is not valid according to the spec.

A recommended way is to use extension elements which are supported by the spec. You can use the BPMN Model API to add an extension element via Java Code.

Does this help you?

Greetings,
Philipp

Thanks Philipp,

I’m new in this field but can partially understand it :slightly_smiling:
The extension elements might be better for me. I tried it adding to the user task:

_ExtensionElements extensionElements = task.getExtensionElements();_

_if (extensionElements == null) {_
_  extensionElements = modelInstance.newInstance(ExtensionElements.class);_
_  task.setExtensionElements(extensionElements);_
_}_
_ CamundaValue typeExtension = modelInstance.newInstance(CamundaValue.class);_
_ extensionElements.getElements().add(typeExtension);_

Through the code I have the extension element in the bpmn. thats great :slight_smile:
I found the String extension to define a String value into it.
But how can I add the value into the extension?

My main goal is to have such a Extension element like :

<extensionElements number="12" otherInformation= "further data"/>

thanks John

Hi John,

I see two options:

  • use an existing element, for example Camunda Properties
ExtensionElements extensionElements = task.getExtensionElements();
if (extensionElements == null) {
 extensionElements = bpmnModelInstance.newInstance(ExtensionElements.class);
 task.setExtensionElements(extensionElements);
}

CamundaProperties camundaProperties = extensionElements.addExtensionElement(CamundaProperties.class);

CamundaProperty camundaProperty = bpmnModelInstance.newInstance(CamundaProperty.class);
camundaProperty.setCamundaName("number");
camundaProperty.setCamundaValue("12");

camundaProperties.addChildElement(camundaProperty);

The result is

<extensionElements>
  <camunda:properties>
    <camunda:property name="number" value="12"/>
  </camunda:properties>
</extensionElements>
ModelElementInstance myExtensionElement = extensionElements.addExtensionElement("http://example.com/bpmn", "myExtensionElement");
myExtensionElement.setAttributeValue("otherInformation", "further data");

And the result is

<extensionElements>
  <ns0:myExtensionElement otherInformation="further data"/>
</extensionElements>

Does one of this options work for you?

Greetings,
Philipp

Hi Philipp,

thank you very much! that works fine for me.

Best regards,
John