2017-10-13 59 views
0

我有下面的XML:JAXB解組屬性引入到一個HashMap

<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進入上面的類,但我不知道如何。

在這裏找到了幾個答案,但沒有一個似乎按照我的需要工作。

回答

1

您應該可以使用適配器類來完成它。這裏是一個工作案例。

Object.java

類有XmlJavaTypeAdapter(BikeAdapter.class) annoted到自行車地圖。適配器和包裝類在這裏定義。

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); 
    } 

}