JAXB - 仅为元素的特定子类型设置 XmlAdapter?
我必须生成一个 xml 元素,该元素可以具有任何“原始类型”(xsd:string、xsd:boolean 等)的值。示例:
<field xsi:type="xsd:string" name="aString">String Value</field>
<field xsi:type="xsd:dateTime" name="aDate">2011-10-21</field>
<field xsi:type="xsd:dateTime" name="aDateTime">2011-10-21T12:00:00</field>
...
因此,我使用此实现让 JAXB 决定原始类型的 xsi:type:
public class Field {
@XmlAttribute
private String name;
@XmlElement
Object value;
}
并且它按预期工作,但所有 java.util.Date 都获取类型 xs: dateTime ...
现在我想仅当“值”对象是 java.util.Date 的实例时才更改编组器的行为以获取如下所示的字段:
<field xsi:type="xsd:date" name="aDate">2011-10-21</field>
<field xsi:type="xsd:dateTime" name="aDateTime">2011-10-21T12:00:00</field>
因此我创建了一个适配器,但是如果我尝试this:
@XmlElement
@XmlJavaTypeAdapter(DateAdapter.class)
Object value;
适配器必须处理java.lang.Object 类型
public class DateAdapter extends XmlAdapter<String, Object> {...}
但我不想松开所有其他对象(整数、双精度等)的 JAXB 编组器...
有一种方法可以为元素的特定子类型设置适配器吗?
I have to generate a xml element that can have as value any "primitive type" (xsd:string, xsd:boolean, etc). Examples:
<field xsi:type="xsd:string" name="aString">String Value</field>
<field xsi:type="xsd:dateTime" name="aDate">2011-10-21</field>
<field xsi:type="xsd:dateTime" name="aDateTime">2011-10-21T12:00:00</field>
...
So, I use this implementation that makes JAXB decide the xsi:type
of the primitive type:
public class Field {
@XmlAttribute
private String name;
@XmlElement
Object value;
}
and it is working as expected but all the java.util.Date gets the type xs:dateTime
...
Now I want to change the behavior of the marshaller ONLY when the 'value' object is an instance of java.util.Date to obtains fields like this:
<field xsi:type="xsd:date" name="aDate">2011-10-21</field>
<field xsi:type="xsd:dateTime" name="aDateTime">2011-10-21T12:00:00</field>
So I create an adapter, but if I try this:
@XmlElement
@XmlJavaTypeAdapter(DateAdapter.class)
Object value;
The adapter must handles a java.lang.Object type
public class DateAdapter extends XmlAdapter<String, Object> {...}
But I do not want to loose the JAXB marshallers for all the others objects (Integer, Double, etc) ...
there is a way to set an adapter for a specific subtype of an element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管我来自 XSD 并从那里生成类,但我也遇到了类似的情况。也就是说,该解决方案也应该适合您。
就我而言,我想扩展 XSD 模型中简单字符串的使用,以便它在 Java 代码中作为 UUID 出现,然后另一个在其他地方作为类出现,依此类推。 XML 方面,都是字符串(xs:string),那么如何将所有这些字符串转换为 Java 方面的单独类型...
我最后做的是,在 XSD 中我为每种情况创建了简单类型,这里是 UUID 声明:
并在需要时使用它代替字符串。
然后,我创建了一个绑定文件,在其中指定了我的 XmlAdaptors,就像您所做的那样,只是现在我有单独的目标,因此可以使用专门针对每个 xml“类型”的多个适配器。
现在,您遇到的另一个困难是,在 Java 端,您的字段的表示只是一个对象。我想一旦你知道了 type 属性,你就可以强制转换它。也许为每种情况创建一个字段的子类,这将允许您更改 typeADaptor 声明。
I faced a similar situation though I was comming from the XSD and generating the classes from there. That said, the solution should be transposable to you as well.
In my cases I wanted to extend the usage of a simple String in the XSD model so that it comes out as a UUID in the Java code, then another would come out as a Class in other places and so on. XML side, it's all string (xs:string), so how to get all these strings into separate types java side...
What I ended doing is, in the XSD I created simple types for each cases, here the UUID declaration :
And use this instead of a String whereever required.
I then created a bindings file where I specified my XmlAdaptors, much as you did, except now I have separate targets and thus can use multiple adaptors targetted specifically to each xml "types".
Now, you have an additionnal difficulty here is that on the Java side the representation of your field is just an object. I suppose you then cast it once you know the type attribute. Perhaps creating a subclass of field for each cases, this would allow you to change the typeADaptor declaration.
尝试
http://jaxb.java.net/guide/Mapping_interfaces.html#Use__XmlJavaTypeAdapter
用于具有多种实现的接口的 XmlJavaTypeAdapter
try
http://jaxb.java.net/guide/Mapping_interfaces.html#Use__XmlJavaTypeAdapter
XmlJavaTypeAdapter for interfaces with multiple implementations