【问题标题】:JAXB unmarshalling issues when elements have namespaces元素具有命名空间时的 JAXB 解组问题
【发布时间】:2026-02-19 02:10:01
【问题描述】:

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

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

我试图在不修改 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();
        }

    }
}

【问题讨论】:

  • 您需要设置限定符标志。 elementFormDefault = XmlNsForm.QUALIFIED 见*.com/questions/14579814/…
  • 我尝试了该问题提供的所有推荐解决方案。不幸的是,这些都没有解决我的问题。

标签: java xml jaxb xml-namespaces unmarshalling


【解决方案1】:

您需要在您的文件中指定 XML 命名空间 URIs @XmlRootelement@XmlElement 注释, 不是 XML 命名空间 prefixes"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,...)。

【讨论】:

    最近更新 更多