简体   繁体   English

JaxB解组属性到哈希图中

[英]JaxB Unmarshalling attributes into a hashmap

I have the following XML: 我有以下XML:

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

So I then have the following Objects: 因此,我有以下对象:

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

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

Bikes 自行车

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

I have tried to unmarshall the xml into the the above classes but I am not sure how. 我试图将xml解组到上述类中,但是我不确定如何。

Found a couple of answers on here but none seemed to work as I needed. 在这里找到了两个答案,但似乎没有一个能按照我的需要工作。

You shall be able to do it using adapter class. 您将能够使用适配器类来做到这一点。 here is a working case. 这是一个工作案例。

Object.java Object.java

The class has XmlJavaTypeAdapter(BikeAdapter.class) annoted to bikes map. 该类具有XmlJavaTypeAdapter(BikeAdapter.class)批注到自行车地图。 Adapter and wrapper class are defined here itself. 适配器和包装器类在此定义。

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

And here is your Main class to test. 这是您要测试的主班。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM