JAXB HashMap无法映射 [英] JAXB HashMap unmappable

查看:178
本文介绍了JAXB HashMap无法映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将POJO类中的HashMap转换为XML。我尝试过使用XmlAdapter,但它只导致HashMap的键和值对作为XML元素的属性。我需要Key作为元素本身,而HashMap的值是元素的值。例如,我需要以下XML:

 <?xml version =1.0encoding =UTF-8standalone ?= 是 > 
< cart>
< supervisor_id> 555< / supervisor_id>
<付款>
< payment sequence =1>
<金额> 123.45< /金额>
< billing_method> 12345< / billing_method>
< form>卡< / form>
< delivery_mode> Q< / delivery_mode>
< /付款>
<付款顺序=2>
<金额> 123.45< /金额>
< person_id> 2333< / person_id>
<表格>现金< / form>
< delivery_mode> Q< / delivery_mode>
< /付款>
< / payments>
< / cart>

我创建了以下类:MyMapType包含MyMapEntryType类的列表,其中包含Key和Value两个字段。如何将Key元素更改为@XmlElement并将值字段赋予Key字段?






这是我的源文件。



MyMapType.java

  import java.util.ArrayList; 
import java.util.List;

public class MyMapType {

private List< MyMapEntryType> entry = new ArrayList< MyMapEntryType>();

公共列表< MyMapEntryType> getEntry(){
返回条目;
}

public void setEntry(List< MyMapEntryType> entry){
this.entry = entry;
}

}

MyMapEntryType.java

  import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
public class MyMapEntryType {
$ b $ @ @mlAttribute
private String key;
@XmlValue
私有字符串值;
public String getKey(){
return key;
}
public void setKey(String key){
this.key = key;
}
public String getValue(){
返回值;
}
public void setValue(String value){
this.value = value;


请另外找到适配器类:



MyMapAdapter.java

  import java.util。 HashMap的; 
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MyMapAdapter extends XmlAdapter< MyMapType,Map< String,String>> {

@Override
MyMapType marshal(Map< String,String> map)抛出异常{

MyMapType myMapType = new MyMapType(); (Entry< String,String> entry:map.entrySet()){
MyMapEntryType myMapEntryType = new MyMapEntryType();


myMapEntryType.setKey(entry.getKey());
myMapEntryType.setValue(entry.getValue());
myMapType.getEntry()。add(myMapEntryType);
}
返回myMapType;
}

@Override
public Map< String,String> unmarshal(MyMapType映射)抛出异常{
HashMap< String,String> hashMap = new HashMap< String,String>(); (MyMapEntryType myEntryType:map.getEntry()){
hashMap.put(myEntryType.getKey(),myEntryType.getValue());
}
返回hashMap;




$ b $ p $这是有HashMap字段的类: / p>

XmlElementMap.java

  @XmlAccessorType (XmlAccessType.FIELD)
public class XmlElementMap {

@XmlAttribute(name =sequence)
private int sequence;

@XmlJavaTypeAdapter(MyMapAdapter.class)
私人地图< String,String> map = new HashMap< String,String>();

public int getSequence(){
return sequence;
}

public void setSequence(int sequence){
this.sequence = sequence;
}

public Map< String,String> getMap(){
return map;


public void setMap(Map< String,String> map){
this.map = map;
}


}



请告知如何实现此目的。



问候,

-Anand



目前它产生以下输出:我有相同的要求我需要钥匙是元素本身并且HashMap的值为元素的值。

我没有使用自定义的适配器,而是通过动态地将HashMap条目转换为列表来实现它的JAXBElement对象,然后使用@XmlAnyElement注释列表。

  @XmlRootElement(name =root)
public class MyMapType {

@ XmlAnyElement
public List< JAXBElement> entries = new ArrayList< JAXBElement>();
$ b $ public MyMapType(){// JAXB required
}
$ b $ public MyMapType(Map< String,String> map){
for(Map。条目< String,String>条目:map.entrySet()){
entries.add(new JAXBElement(new QName(entry.getKey()),
String.class,entry.getValue()) );



public static void main(String [] args)throws Exception {
JAXBContext context = JAXBContext.newInstance(MyMapType.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

地图< String,String> map = new LinkedHashMap< String,String>();
map.put(key1,value1);
map.put(key2,value2);
MyMapType mt = new MyMapType(map);

marshaller.marshal(mt,System.out);




$ b $ p
$输出是

 <?xml version =1.0encoding =UTF-8standalone =yes?> 
< root>
< / key1> value1< / key1>
< key2> value2< / key2>
< / root>


I want to convert a HashMap in a POJO class to XML. I tried using the XmlAdapter but it results in only the key and value pairs of the HashMap being the attributes of the XML Elements. I need the Key to be the Element itself and the value of the HashMap to be the value of the element. For instance, I need the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cart>
<supervisor_id>555</supervisor_id>
<payments>
    <payment sequence="1">
        <amount>123.45</amount>
        <billing_method>12345</billing_method>
        <form>card</form>
        <delivery_mode>Q</delivery_mode>
    </payment>
<payment sequence="2">
        <amount>123.45</amount>
        <person_id>2333</person_id>
        <form>cash</form>
        <delivery_mode>Q</delivery_mode>
    </payment>
</payments>
</cart>

I created the following classes: MyMapType holds a list of MyMapEntryType class which has two fields namely Key and Value. How do I change the Key element to be @XmlElement and assign the value field to the Key field?


Here are my source files.

MyMapType.java

import java.util.ArrayList;
import java.util.List;

public class MyMapType {

    private List<MyMapEntryType> entry = new ArrayList<MyMapEntryType>();

    public List<MyMapEntryType> getEntry() {
        return entry;
    }

    public void setEntry(List<MyMapEntryType> entry) {
        this.entry = entry;
    }

}

MyMapEntryType.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
public class MyMapEntryType {

@XmlAttribute
private String key;
@XmlValue
private String 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;
}
}

Please also find the adapter class:

MyMapAdapter.java

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MyMapAdapter extends XmlAdapter<MyMapType, Map<String, String>> {

    @Override
   public MyMapType marshal(Map<String, String> map) throws Exception {

        MyMapType myMapType = new MyMapType();

      for(Entry<String, String> entry : map.entrySet()) {
         MyMapEntryType myMapEntryType = new MyMapEntryType();
         myMapEntryType.setKey(entry.getKey());
         myMapEntryType.setValue(entry.getValue());
         myMapType.getEntry().add(myMapEntryType);
      }
      return myMapType;
   }

   @Override
   public Map<String, String> unmarshal(MyMapType map) throws Exception {
      HashMap<String, String> hashMap = new HashMap<String, String>();
      for(MyMapEntryType myEntryType : map.getEntry()) {
         hashMap.put(myEntryType.getKey(), myEntryType.getValue());
      }
      return hashMap;
   }
 }

This is the class which has the HashMap field:

XmlElementMap.java

@XmlAccessorType(XmlAccessType.FIELD)
public class XmlElementMap {

@XmlAttribute(name="sequence")
private int sequence;

@XmlJavaTypeAdapter(MyMapAdapter.class)
private Map<String, String> map = new HashMap<String, String>();

public int getSequence() {
    return sequence;
}

public void setSequence(int sequence) {
    this.sequence = sequence;
}

public Map<String, String> getMap() {
    return map;
}

public void setMap(Map<String, String> map) {
    this.map = map;
}


}


Please advise on how to achieve this.

Regards,
-Anand

Currently it produces the following output:

解决方案

I have the same requirement "I need the Key to be the Element itself and the value of the HashMap to be the value of the element".

I didn't use customized adapter, but implemented it by converting the HashMap entries dynamically to a list of JAXBElement objects, and then annotated the list with @XmlAnyElement.

@XmlRootElement(name="root")
public class MyMapType {

    @XmlAnyElement
    public List<JAXBElement> entries = new ArrayList<JAXBElement>();

    public MyMapType() {    // JAXB required    
    }

    public MyMapType(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            entries.add(new JAXBElement(new QName(entry.getKey()), 
                    String.class, entry.getValue()));
        }
    }

    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance(MyMapType.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        MyMapType mt = new MyMapType(map);

        marshaller.marshal(mt, System.out); 
    }
}

The output is,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <key1>value1</key1>
    <key2>value2</key2>
</root>

这篇关于JAXB HashMap无法映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆