为什么 JAXB 有时会映射到 JAXBElement? [英] Why does JAXB sometimes map to JAXBElement?
问题描述
在非官方指南中有一个占位符答案,其中包含指向一篇(在我看来)似乎毫不相关的文章.
There is a placeholder answer over at the unofficial guide with a link to an article which (to me) seems quite unrelated.
我使用 XJC 生成我的 JAXB 类,虽然它们中的大多数按预期相互映射,但有些元素被映射到 JAXBElement
.这对于带循环的图来说是最烦人的,有时 Foo 元素的父节点将是 JAXBElement
,它本身没有父属性,从而打破了循环.
I use XJC to generate my JAXB classes and while most of them map to each other as expected, some elements get mapped to JAXBElement<Foo>
. This is most annoying for graphs with cycles, where sometimes the parent node of a Foo element will be the JAXBElement<Foo>
, which doesn't itself have a parent property, breaking the cycle.
我可以想到各种解决方法,但如果有人能向我解释这种行为会更好.为什么 JAXB 有时会将
元素映射到 JAXBElement
而不是 Foo?
I can think of various workarounds, but it would be much nicer if someone could explain this behaviour to me. Why does JAXB sometimes map a <Foo>
element to JAXBElement<Foo>
instead of Foo?
推荐答案
JAXBElement 用于在对象模型中没有足够信息的用例中保留元素名称/命名空间.最常见的是替换组:
JAXBElement is used to preserve the element name/namespace in use cases where enough information is not present in the object model. The most common occurence is with substitution groups:
使用替换组:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org"
xmlns="http://www.example.org"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="anElement"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="anElement" type="xs:string"/>
<xs:element name="aSubstituteElement" type="xs:string" substitutionGroup="anElement"/>
</xs:schema>
将产生:
package org.example;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"anElement"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElementRef(name = "anElement", namespace = "http://www.example.org", type = JAXBElement.class)
protected JAXBElement<String> anElement;
public JAXBElement<String> getAnElement() {
return anElement;
}
public void setAnElement(JAXBElement<String> value) {
this.anElement = ((JAXBElement<String> ) value);
}
}
无替换组:
如果删除替换组:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org"
xmlns="http://www.example.org"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="anElement"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="anElement" type="xs:string"/>
</xs:schema>
将生成以下类:
package org.example;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"anElement"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected String anElement;
public String getAnElement() {
return anElement;
}
public void setAnElement(String value) {
this.anElement = value;
}
}
您在解组时也可能得到一个 JAXBElement,请比较以下示例:
You may also get a JAXBElement when you unmarshal, compare the following examples:
这篇关于为什么 JAXB 有时会映射到 JAXBElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!