Reputation: 57
This code only works if I remove "bpmn:" from all the elements inside the XML file, otherwise it throws the following exception:
javax.xml.bind.UnmarshalException: unexpected Element (URI:"http://www.omg.org/spec/BPMN/20100524/MODEL", local:"definitions"). Expected elements are <{}definitions> .
Im trying to make it work without modifying the XML file. Any help would be greatly appreciated. Thanks in advance :)
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_13d3a6z" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.1.1">
<bpmn:process id="Process_1tovjba" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_06i118e</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:task id="Activity_1d3friu" name="Task 1">
<bpmn:incoming>Flow_06i118e</bpmn:incoming>
<bpmn:outgoing>Flow_0linmbs</bpmn:outgoing>
</bpmn:task>
<bpmn:sequenceFlow id="Flow_06i118e" sourceRef="StartEvent_1" targetRef="Activity_1d3friu" />
<bpmn:task id="Activity_1e17g78" name="Task 2">
<bpmn:incoming>Flow_0linmbs</bpmn:incoming>
<bpmn:outgoing>Flow_0yu7ggy</bpmn:outgoing>
</bpmn:task>
<bpmn:intermediateThrowEvent id="Event_1tlw9ds">
<bpmn:incoming>Flow_0yu7ggy</bpmn:incoming>
</bpmn:intermediateThrowEvent>
<bpmn:sequenceFlow id="Flow_0yu7ggy" sourceRef="Activity_1e17g78" targetRef="Event_1tlw9ds" />
<bpmn:sequenceFlow id="Flow_0linmbs" sourceRef="Activity_1d3friu" targetRef="Activity_1e17g78" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1tovjba">
<bpmndi:BPMNEdge id="Flow_06i118e_di" bpmnElement="Flow_06i118e">
<di:waypoint x="215" y="117" />
<di:waypoint x="360" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0yu7ggy_di" bpmnElement="Flow_0yu7ggy">
<di:waypoint x="840" y="117" />
<di:waypoint x="931" y="117" />
<di:waypoint x="931" y="190" />
<di:waypoint x="1022" y="190" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0linmbs_di" bpmnElement="Flow_0linmbs">
<di:waypoint x="460" y="117" />
<di:waypoint x="600" y="117" />
<di:waypoint x="600" y="90" />
<di:waypoint x="740" y="90" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1d3friu_di" bpmnElement="Activity_1d3friu">
<dc:Bounds x="360" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1e17g78_di" bpmnElement="Activity_1e17g78">
<dc:Bounds x="740" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1tlw9ds_di" bpmnElement="Event_1tlw9ds">
<dc:Bounds x="1022" y="172" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
@XmlRootElement
public class Definitions {
private String id;
private Process process;
public Definitions(){};
public Definitions(String id, Process process){
this.id = id;
this.process = process;
}
@XmlAttribute
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(namespace = "bpmn",name = "process")
public Process getProcess() {
return process;
}
public void setProcess(Process process) {
this.process = process;
}
}
public class XMLToObject {
public static void main(String[] args) {
try {
File file = new File("process.bpmn");
JAXBContext jaxbContext = JAXBContext.newInstance(Definitions.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Definitions definitions= (Definitions) jaxbUnmarshaller.unmarshal(file);
System.out.println(definitions.getId());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Upvotes: 3
Views: 2767
Reputation: 10147
You need to specify the XML namespace URIs in your
@XmlRootelement
and @XmlElement
annotations,
not the XML namespace prefixes ("bpmn"
or "bpmndi"
).
So then your Definitions
class would look like this:
@XmlRootElement(namespace = "http://www.omg.org/spec/BPMN/20100524/MODEL")
public class Definitions {
private String id;
private Process process;
private BPMNDiagram bpmnDiagram;
public Definitions(){};
public Definitions(String id, Process process, BPMNDiagram bpmnDiagram){
this.id = id;
this.process = process;
this.bpmnDiagram = bpmnDiagram;
}
@XmlAttribute
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(namespace = "http://www.omg.org/spec/BPMN/20100524/MODEL", name = "process")
public Process getProcess() {
return process;
}
public void setProcess(Process process) {
this.process = process;
}
@XmlElement(namespace = "http://www.omg.org/spec/BPMN/20100524/DI", name = "BPMNDiagram")
public BPMNDiagram getBpmnDiagram() {
return bpmnDiagram;
}
public void setBpmnDiagram(BPMNDiagram bpmnDiagram) {
this.bpmnDiagram = bpmnDiagram;
}
}
In a similar way you need to modify the @XmlElement
annotations in your
other classes (Process
, BPMNDiagram
, ...).
Upvotes: 1