英文:
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
<?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();
}
}
}
答案1
得分: 1
你需要在@XmlRootelement
和@XmlElement
注解中指定XML命名空间URIs,而不是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;
}
}
类似地,你需要修改你的其他类(Process
、BPMNDiagram
等)中的@XmlElement
注解。
英文:
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
, ...).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论