Java 如何在JAXB中将对象作为@XmlValue嵌入?
我想用XML表示Java 如何在JAXB中将对象作为@XmlValue嵌入?,java,jaxb,Java,Jaxb,我想用XML表示List的一个实例,如下所示: <row> <entry key="key1" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">value1</entry> <entry key="key2" xsi:type="xs:string" xm
List
的一个实例,如下所示:
<row>
<entry key="key1" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">value1</entry>
<entry key="key2" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">value2</entry>
<entry key="key3" />
<entry key="keyDate" xsi:type="xs:dateTime" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2012-08-19T13:00:59.412-04:00</entry>
</row>
以及以下测试代码:
package net.parkerson.test1;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import net.parkerson.test1.JAXBMapAdapter;
public class Test1
{
@XmlRootElement
public static class Thingy
{
@XmlElements(@XmlElement(name = "row"))
@XmlJavaTypeAdapter(type=Map.class, value=JAXBMapAdapter.class)
public List<Map<String,Object>> rows;
}
public static void main(String[] args) throws Exception
{
Test1.Thingy thingy = new Test1.Thingy();
Map<String,Object> thing1 = new HashMap<String,Object>();
thing1.put("foo", "bar");
thing1.put("date", new Date());
Map<String,Object> thing2 = new HashMap<String,Object>();
thing2.put("foo", "baz");
thing2.put("date", new Date());
List<Map<String,Object>> things = new ArrayList<Map<String,Object>>();
things.add(thing1);
things.add(thing2);
thingy.rows = things;
java.io.StringWriter sw = new java.io.StringWriter();
JAXB.marshal(thingy, sw);
System.out.print(sw.toString());
}
}
我确信有一种方法可以使用基于
XmlAdapter
的代码来实现这一点,但我想我已经盯着这个太久了,想不出来了 奇怪。如果将地图类型从
更改为
,它将按预期工作
更新:
似乎如果您强制JAXB使用@XmlValue(或@XmlAttribute)呈现值,它就没有地方放置其类型信息:xsi:type=“xs:dateTime”…
它需要是@XmlElement
我的建议是使用带有适配器的映射
,该适配器根据基础类型从字符串
到对象
进行编码/解码:
public class MySmartAdapter extends XmlAdapter<String, Object> {
@Override
public Object unmarshal(String s) throws Exception {
//check if s is date and return date
//check for other types
return s;
}
@Override
public String marshal(Object o) throws Exception {
return o != null ? o.toString() : null;
}
}
public static class JAXBMapEntry
{
@XmlAttribute
public String key;
@XmlValue
@XmlJavaTypeAdapter(MySmartAdapter.class)
public Object value;
public JAXBMapEntry()
{
}
public JAXBMapEntry(Map.Entry<String, Object> entry)
{
key = entry.getKey();
value = entry.getValue();
}
}
公共类MySmartAdapter扩展了XmlAdapter{
@凌驾
公共对象解组(字符串s)引发异常{
//检查s是否为日期和返回日期
//检查其他类型
返回s;
}
@凌驾
公共字符串封送处理(对象o)引发异常{
返回o!=null?o.toString():null;
}
}
公共静态类JAXBMapEntry
{
@XmlAttribute
公共字符串密钥;
@XmlValue
@XmlJavaTypeAdapter(MySmartAdapter.class)
公共客体价值;
公共JAXBMapEntry()
{
}
公共JAXBMapEntry(Map.Entry)
{
key=entry.getKey();
value=entry.getValue();
}
}
我想我应该注意,关键是要将
对象
封送为值类型,因此基于JAXB的内部封送,存在正确的模式类型。是的,但是不能将键指定为属性。我用一个建议更新了我的答案;我会玩一玩,看看能不能从这里到那里。如果是这样,你就赢了D
<thingy>
<row>
<entry key="foo">
<value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">bar</value>
</entry>
<entry key="date">
<value xsi:type="xs:dateTime" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2012-08-19T13:20:57.484-04:00</value>
</entry>
</row>
<row>
<entry key="foo">
<value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">baz</value>
</entry>
<entry key="date">
<value xsi:type="xs:dateTime" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2012-08-19T13:20:57.484-04:00</value>
</entry>
</row>
</thingy>
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154)
at com.sun.xml.internal.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:66)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:95)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:97)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:97)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at javax.xml.bind.JAXB$Cache.<init>(JAXB.java:87)
at javax.xml.bind.JAXB.getContext(JAXB.java:114)
at javax.xml.bind.JAXB._marshal(JAXB.java:538)
at javax.xml.bind.JAXB.marshal(JAXB.java:431)
at net.parkerson.test1.Test1.main(Test1.java:49)
public class MySmartAdapter extends XmlAdapter<String, Object> {
@Override
public Object unmarshal(String s) throws Exception {
//check if s is date and return date
//check for other types
return s;
}
@Override
public String marshal(Object o) throws Exception {
return o != null ? o.toString() : null;
}
}
public static class JAXBMapEntry
{
@XmlAttribute
public String key;
@XmlValue
@XmlJavaTypeAdapter(MySmartAdapter.class)
public Object value;
public JAXBMapEntry()
{
}
public JAXBMapEntry(Map.Entry<String, Object> entry)
{
key = entry.getKey();
value = entry.getValue();
}
}