我需要将列表(小工具)转换为地图。因此使用XmlAdapter。
模式
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://jaxb.com/schema" xmlns:test="http://jaxb.com/schema">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="gadgets" type="test:GadgetMapModeller" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="GadgetMapModeller">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="value" type="test:gadget"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="gadget">
<xs:sequence>
<xs:element name="make" type="xs:string"/>
<xs:element name="model" type="xs:string"/>
<xs:element name="year" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Computer">
<xs:complexContent>
<xs:extension base="test:gadget">
<xs:sequence>
<xs:element name="speed" type="xs:int"/>
<xs:element name="cpu" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
适配器
package com.jaxb;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.jaxb.schema.Gadget;
import com.jaxb.schema.GadgetMapModeller;
public class GadgetMapAdapter extends XmlAdapter<GadgetMapModeller, GadgetMap<String,Gadget>>
{
@Override
public GadgetMap<String,Gadget> unmarshal(GadgetMapModeller modeller)
{
GadgetMap<String,Gadget> map = new GadgetMap<String,Gadget>();
for (GadgetMapModeller.Entry e : modeller.getEntry())
{
map.put(e.getKey(), e.getValue());
}
return map;
}
@Override
public GadgetMapModeller marshal(GadgetMap<String,Gadget> map)
{
GadgetMapModeller modeller = new GadgetMapModeller();
for (Map.Entry<String,Gadget> entry : map.entrySet())
{
GadgetMapModeller.Entry e = new GadgetMapModeller.Entry();
e.setKey(entry.getKey());
e.setValue(entry.getValue());
modeller.getEntry().add(e);
System.out.println("entry.getValue()" + entry.getValue());
}
return modeller;
}
}
GadgetMap
package com.jaxb;
import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlJavaTypeAdapter(GadgetMapAdapter.class)
public class GadgetMap<String,Gadget> extends HashMap<String,Gadget>
{
}
在调试时,我发现调用了XmlAdapter.marshal方法&amp;我可以看到里面对象的价值。但收到的输出如下。
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Person xmlns:ns2="http://jaxb.com/schema">
<ns2:name>Matt Shannon</ns2:name>
<ns2:gadgets>
<entry>
<key>my ipad</key>
<value>
<ns2:make>Apple</ns2:make>
<ns2:model>iPod</ns2:model>
<ns2:year>2002</ns2:year>
</value>
</entry>
<entry>
<key>my laptop</key>
<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Computer">
com.jaxb.Computer@704f568
</value>
</entry>
</ns2:gadgets>
</ns2:Person>
为什么它没有正确编组?生产 - com.jaxb.Computer@704f568 ?你能帮我找一下吗?
答案 0 :(得分:0)
你是否正确地初始化了JAXBContext(它必须知道计算机),简单就是
@XmlSeeAlso(Computer.class)
public class Gadget {
...
}