我有以下XML:
<object>
<name>Test</name>
<bikes>
<bike key="Hello" value="World"/>
</bikes>
</object>
所以我有以下对象:
@XmlRootElement
public class Object {
@XmlElement
private String name;
@XmlElement
private Bikes bikes;
public Object(String name, Bikes bikes) {
this.name = name;
this.bikes = bikes;
}
自行车
public class Bikes{
private Map<String, String> bike = new HashMap();
@XmlElement
public Bikes(Map<String, String> bike) {
this.bike = bike;
}
我试图将xml解组为上面的类,但我不确定如何。
在这里找到了几个答案,但似乎没有一个能够正常工作。
答案 0 :(得分:1)
您应该可以使用适配器类来完成它。这是一个工作案例。
Object.java
该班有XmlJavaTypeAdapter(BikeAdapter.class)
注释到自行车地图。适配器和包装器类在这里定义。
package testjaxb;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Object {
@XmlElement
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getBikes() {
return bikes;
}
public void setBikes(Map<String, String> bikes) {
this.bikes = bikes;
}
@XmlJavaTypeAdapter(BikeAdapter.class)
private Map<String, String> bikes;
public Object() {
}
}
class BikeWrapper {
@XmlElement(name = "bike")
List<Bike> bike = new ArrayList<Bike>();
}
class BikeAdapter extends XmlAdapter<BikeWrapper, Map<String, String>> {
public BikeWrapper marshal(Map<String, String> arg0) throws Exception {
BikeWrapper bw = new BikeWrapper();
List<Bike> bikes = new ArrayList<Bike>();
for (Map.Entry<String, String> entry : arg0.entrySet()) {
bikes.add(new Bike(entry.getKey(), entry.getValue()));
}
bw.bike = bikes;
return bw;
}
public Map<String, String> unmarshal(BikeWrapper arg0) throws Exception {
Map<String, String> r = new HashMap<String, String>();
for (Bike mapelement : arg0.bike) {
r.put(mapelement.getKey(), mapelement.getValue());
}
return r;
}
}
Bike.jaa
package testjaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bike {
@XmlAttribute()
private String key;
public Bike() {
}
public Bike(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@XmlAttribute()
private String value;
public String toString() {
return "Bike : key-" + getKey() + ", value -" + getValue();
}
}
这是您要测试的主要课程。
package testjaxb;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args) throws Exception {
String xmlString = "<object>\n"
+ " <name>Test</name>\n"
+ " <bikes>\n"
+ " <bike key=\"Hello\" value=\"World\"/>\n"
+ " </bikes>\n"
+ "</object>";
testjaxb.Object o = unmarshal(testjaxb.Object.class, xmlString);
System.out.println("Bike List.." + o.getBikes());
}
private static <C> C unmarshal(Class<C> c, String sampleXML) throws Exception {
JAXBContext jc = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(sampleXML);
//System.out.println("" + sampleXML);
return (C) unmarshaller.unmarshal(reader);
}
}