元素具有名称空间时的JAXB解组问题

问题描述

仅当我从XML文件中的所有元素中删除“ bpmn:”时,此代码才起作用,否则它将引发以下异常:

javax.xml.bind.UnmarshalException:意外元素(URI:“ http://www.omg.org/spec/BPMN/20100524/MODEL”,本地:“ deFinitions”)。期望的元素是。

我正在尝试使其工作而不修改XML文件。任何帮助将不胜感激。在此先感谢:)

<?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();
        }

    }
}

解决方法

您需要在自己的服务器中指定XML名称空间 URI @XmlRootelement@XmlElement注释, 不是XML名称空间前缀"bpmn""bpmndi")。

因此,您的Definitions类将如下所示:

@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;
    }   
}

以类似的方式,您需要修改自己的@XmlElement批注 其他类(ProcessBPMNDiagram,...)。