2017-10-13 2 views
0

나는 다음과 같은 XML이 : 나는 비 직렬화하려고 노력JAXB 비 정렬 화는 해시 맵에 속성

public class Bikes{ 
     private Map<String, String> bike = new HashMap(); 
     @XmlElement 
     public Bikes(Map<String, String> bike) { 
      this.bike = bike; 
     } 

@XmlRootElement 
    public class Object { 
     @XmlElement 
     private String name; 
     @XmlElement 
     private Bikes bikes; 

     public Object(String name, Bikes bikes) { 
       this.name = name; 
       this.bikes = bikes; 
     } 

자전거 :

<object> 
    <name>Test</name> 
    <bikes> 
     <bike key="Hello" value="World"/> 
    </bikes> 
</object> 

그래서 나는 다음 개체가를 위의 클래스에 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); 
    } 

}