为什么 xs:any 不与 jaxb class 自动绑定?
Why does xs:any not autobind with jaxb class?
我得到了一个 xsd 并从中生成了 JaxB-类(例如 ApplicationCustomType)。一些 classes 有一个 xs:any 作为元素。我可以向这些字段添加内容(例如 xs:any)。编组工作良好。
但是当我尝试解组它时
FullContent contentType = XmlObjectHelper.getXmlTypeFromString(contentType, FullContent.class);
JaxB-classes 没有填充来自每个 xs:any 的字段。所有其他字段都按应有的方式填写,但 xs:any 的绑定似乎不起作用。
我读了那个 serializing-with-jaxb 的回答,它看起来很像我想我没有忘记什么。
我也尝试添加 lax = true 但它又没有解组我的 xml.
我做错了什么或者忘记了什么?
public class ApplicationCustomType {
@XmlAnyElement
protected List<Element> any;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
示例 xs:any 元素。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addressAttachment", propOrder = {
"locale",
"ourUserId"
})
public class AddressAttachment {
@XmlElement(required = false)
protected String locale;
@XmlElement(required = false)
protected String ourUserId;
}
ObjectFactory.java
@XmlElementDecl(namespace = "http://***/xsd/addressAttachment/v1", name = "information")
public JAXBElement<AddressAttachment> createAddressAttachment(AddressAttachment value) {
return new JAXBElement<AddressAttachment>(_AddressAttachment_QNAME, AddressAttachment.class, null, value);
}
我收到的xml:
<content>
<applicationCustom>
<addressAttachment>
<locale>CH.de</locale>
<ourUserId>264646337383839</ourUserId>
</addressAttachment>
</applicationCustom>
</content>
解法:
我们在列表顶部使用 (lax = true) 重试了它,而不是 class。
public class ApplicationCustomType {
@XmlAnyElement(lax = true)
protected List<Element> any;
如果使用<Element>
或<Object>
不影响结果。使用 Element
时,您实际上不必向编组器提供 class。但是在这两种情况下,对于要添加到 xs:any 列表中的每个对象,您都需要为对象工厂中的每个 XMLRoot
class 添加 @XmlElementDecl
。
尝试这样使用它:
@XmlAnyElement
protected List<Object> any;
看看里面是什么!
解释在这里:https://dzone.com/articles/jaxbs-xmlanyelementlaxtrue
你还应该提供已知的 类(AddressAttachment.class...) 给解组器,否则它无法找出 xml 中的内容对吗?
您的示例中未显示,但应如下所示:
JAXBContext.newInstance(FullContent.class,AddressAttachment.class ...);
对于 Any,根对象本身没有引用。
与 Jaxb 共度美好时光
我得到了一个 xsd 并从中生成了 JaxB-类(例如 ApplicationCustomType)。一些 classes 有一个 xs:any 作为元素。我可以向这些字段添加内容(例如 xs:any)。编组工作良好。
但是当我尝试解组它时
FullContent contentType = XmlObjectHelper.getXmlTypeFromString(contentType, FullContent.class);
JaxB-classes 没有填充来自每个 xs:any 的字段。所有其他字段都按应有的方式填写,但 xs:any 的绑定似乎不起作用。
我读了那个 serializing-with-jaxb 的回答,它看起来很像我想我没有忘记什么。 我也尝试添加 lax = true 但它又没有解组我的 xml.
我做错了什么或者忘记了什么?
public class ApplicationCustomType {
@XmlAnyElement
protected List<Element> any;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
示例 xs:any 元素。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addressAttachment", propOrder = {
"locale",
"ourUserId"
})
public class AddressAttachment {
@XmlElement(required = false)
protected String locale;
@XmlElement(required = false)
protected String ourUserId;
}
ObjectFactory.java
@XmlElementDecl(namespace = "http://***/xsd/addressAttachment/v1", name = "information")
public JAXBElement<AddressAttachment> createAddressAttachment(AddressAttachment value) {
return new JAXBElement<AddressAttachment>(_AddressAttachment_QNAME, AddressAttachment.class, null, value);
}
我收到的xml:
<content>
<applicationCustom>
<addressAttachment>
<locale>CH.de</locale>
<ourUserId>264646337383839</ourUserId>
</addressAttachment>
</applicationCustom>
</content>
解法:
我们在列表顶部使用 (lax = true) 重试了它,而不是 class。
public class ApplicationCustomType {
@XmlAnyElement(lax = true)
protected List<Element> any;
如果使用<Element>
或<Object>
不影响结果。使用 Element
时,您实际上不必向编组器提供 class。但是在这两种情况下,对于要添加到 xs:any 列表中的每个对象,您都需要为对象工厂中的每个 XMLRoot
class 添加 @XmlElementDecl
。
尝试这样使用它:
@XmlAnyElement
protected List<Object> any;
看看里面是什么!
解释在这里:https://dzone.com/articles/jaxbs-xmlanyelementlaxtrue
你还应该提供已知的 类(AddressAttachment.class...) 给解组器,否则它无法找出 xml 中的内容对吗?
您的示例中未显示,但应如下所示:
JAXBContext.newInstance(FullContent.class,AddressAttachment.class ...);
对于 Any,根对象本身没有引用。
与 Jaxb 共度美好时光