JAXB在元素具有命名空间时的取消编组问题

huangapple go评论70阅读模式
英文:

JAXB unmarshalling issues when elements have namespaces

问题

这段代码只有在我从XML文件中的所有元素中删除“bpmn:”时才有效,否则会抛出以下异常:

javax.xml.bind.UnmarshalException: 非预期元素 (URI:"http://www.omg.org/spec/BPMN/20100524/MODEL", 本地:"definitions")。预期的元素是<{}definitions>。

我试图让它在不修改XML文件的情况下工作。非常感谢提前的帮助!

英文:

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 JAXB在元素具有命名空间时的取消编组问题

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;bpmn:definitions xmlns:bpmn=&quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot; xmlns:bpmndi=&quot;http://www.omg.org/spec/BPMN/20100524/DI&quot; xmlns:dc=&quot;http://www.omg.org/spec/DD/20100524/DC&quot; xmlns:di=&quot;http://www.omg.org/spec/DD/20100524/DI&quot; id=&quot;Definitions_13d3a6z&quot; targetNamespace=&quot;http://bpmn.io/schema/bpmn&quot; exporter=&quot;Camunda Modeler&quot; exporterVersion=&quot;4.1.1&quot;&gt;
  &lt;bpmn:process id=&quot;Process_1tovjba&quot; isExecutable=&quot;true&quot;&gt;
    &lt;bpmn:startEvent id=&quot;StartEvent_1&quot;&gt;
      &lt;bpmn:outgoing&gt;Flow_06i118e&lt;/bpmn:outgoing&gt;
    &lt;/bpmn:startEvent&gt;
    &lt;bpmn:task id=&quot;Activity_1d3friu&quot; name=&quot;Task 1&quot;&gt;
      &lt;bpmn:incoming&gt;Flow_06i118e&lt;/bpmn:incoming&gt;
      &lt;bpmn:outgoing&gt;Flow_0linmbs&lt;/bpmn:outgoing&gt;
    &lt;/bpmn:task&gt;
    &lt;bpmn:sequenceFlow id=&quot;Flow_06i118e&quot; sourceRef=&quot;StartEvent_1&quot; targetRef=&quot;Activity_1d3friu&quot; /&gt;
    &lt;bpmn:task id=&quot;Activity_1e17g78&quot; name=&quot;Task 2&quot;&gt;
      &lt;bpmn:incoming&gt;Flow_0linmbs&lt;/bpmn:incoming&gt;
      &lt;bpmn:outgoing&gt;Flow_0yu7ggy&lt;/bpmn:outgoing&gt;
    &lt;/bpmn:task&gt;
    &lt;bpmn:intermediateThrowEvent id=&quot;Event_1tlw9ds&quot;&gt;
      &lt;bpmn:incoming&gt;Flow_0yu7ggy&lt;/bpmn:incoming&gt;
    &lt;/bpmn:intermediateThrowEvent&gt;
    &lt;bpmn:sequenceFlow id=&quot;Flow_0yu7ggy&quot; sourceRef=&quot;Activity_1e17g78&quot; targetRef=&quot;Event_1tlw9ds&quot; /&gt;
    &lt;bpmn:sequenceFlow id=&quot;Flow_0linmbs&quot; sourceRef=&quot;Activity_1d3friu&quot; targetRef=&quot;Activity_1e17g78&quot; /&gt;
  &lt;/bpmn:process&gt;
  &lt;bpmndi:BPMNDiagram id=&quot;BPMNDiagram_1&quot;&gt;
    &lt;bpmndi:BPMNPlane id=&quot;BPMNPlane_1&quot; bpmnElement=&quot;Process_1tovjba&quot;&gt;
      &lt;bpmndi:BPMNEdge id=&quot;Flow_06i118e_di&quot; bpmnElement=&quot;Flow_06i118e&quot;&gt;
        &lt;di:waypoint x=&quot;215&quot; y=&quot;117&quot; /&gt;
        &lt;di:waypoint x=&quot;360&quot; y=&quot;117&quot; /&gt;
      &lt;/bpmndi:BPMNEdge&gt;
      &lt;bpmndi:BPMNEdge id=&quot;Flow_0yu7ggy_di&quot; bpmnElement=&quot;Flow_0yu7ggy&quot;&gt;
        &lt;di:waypoint x=&quot;840&quot; y=&quot;117&quot; /&gt;
        &lt;di:waypoint x=&quot;931&quot; y=&quot;117&quot; /&gt;
        &lt;di:waypoint x=&quot;931&quot; y=&quot;190&quot; /&gt;
        &lt;di:waypoint x=&quot;1022&quot; y=&quot;190&quot; /&gt;
      &lt;/bpmndi:BPMNEdge&gt;
      &lt;bpmndi:BPMNEdge id=&quot;Flow_0linmbs_di&quot; bpmnElement=&quot;Flow_0linmbs&quot;&gt;
        &lt;di:waypoint x=&quot;460&quot; y=&quot;117&quot; /&gt;
        &lt;di:waypoint x=&quot;600&quot; y=&quot;117&quot; /&gt;
        &lt;di:waypoint x=&quot;600&quot; y=&quot;90&quot; /&gt;
        &lt;di:waypoint x=&quot;740&quot; y=&quot;90&quot; /&gt;
      &lt;/bpmndi:BPMNEdge&gt;
      &lt;bpmndi:BPMNShape id=&quot;_BPMNShape_StartEvent_2&quot; bpmnElement=&quot;StartEvent_1&quot;&gt;
        &lt;dc:Bounds x=&quot;179&quot; y=&quot;99&quot; width=&quot;36&quot; height=&quot;36&quot; /&gt;
      &lt;/bpmndi:BPMNShape&gt;
      &lt;bpmndi:BPMNShape id=&quot;Activity_1d3friu_di&quot; bpmnElement=&quot;Activity_1d3friu&quot;&gt;
        &lt;dc:Bounds x=&quot;360&quot; y=&quot;77&quot; width=&quot;100&quot; height=&quot;80&quot; /&gt;
      &lt;/bpmndi:BPMNShape&gt;
      &lt;bpmndi:BPMNShape id=&quot;Activity_1e17g78_di&quot; bpmnElement=&quot;Activity_1e17g78&quot;&gt;
        &lt;dc:Bounds x=&quot;740&quot; y=&quot;77&quot; width=&quot;100&quot; height=&quot;80&quot; /&gt;
      &lt;/bpmndi:BPMNShape&gt;
      &lt;bpmndi:BPMNShape id=&quot;Event_1tlw9ds_di&quot; bpmnElement=&quot;Event_1tlw9ds&quot;&gt;
        &lt;dc:Bounds x=&quot;1022&quot; y=&quot;172&quot; width=&quot;36&quot; height=&quot;36&quot; /&gt;
      &lt;/bpmndi:BPMNShape&gt;
    &lt;/bpmndi:BPMNPlane&gt;
  &lt;/bpmndi:BPMNDiagram&gt;
&lt;/bpmn:definitions&gt;
@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 = &quot;bpmn&quot;,name = &quot;process&quot;)
    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(&quot;process.bpmn&quot;);
            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();
        }

    }
}

答案1

得分: 1

你需要在@XmlRootelement@XmlElement注解中指定XML命名空间URIs,而不是XML命名空间前缀&quot;bpmn&quot;&quot;bpmndi&quot;)。

因此,你的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;
    }   
}

类似地,你需要修改你的其他类(ProcessBPMNDiagram等)中的@XmlElement注解。

英文:

You need to specify the XML namespace URIs in your
@XmlRootelement and @XmlElement annotations,
not the XML namespace prefixes (&quot;bpmn&quot; or &quot;bpmndi&quot;).

So then your Definitions class would look like this:

@XmlRootElement(namespace = &quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot;)
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 = &quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot;, name = &quot;process&quot;)
    public Process getProcess() {
        return process;
    }

    public void setProcess(Process process) {
        this.process = process;
    }

    @XmlElement(namespace = &quot;http://www.omg.org/spec/BPMN/20100524/DI&quot;, name = &quot;BPMNDiagram&quot;)
    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, ...).

huangapple
  • 本文由 发表于 2020年8月11日 00:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63344454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定