gpt4 book ai didi

java - JAXB HashMap 不可映射

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:37:43 26 4
gpt4 key购买 nike

我想将 POJO 类中的 HashMap 转换为 XML。我尝试使用 XmlAdapter,但它只会导致 HashMap 的键和值对成为 XML 元素的属性。我需要 Key 是 Element 本身,而 HashMap 的值是元素的值。例如,我需要以下 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>

我创建了以下类:MyMapType 包含一个 MyMapEntryType 类列表,它有两个字段,即键和值。如何将 Key 元素更改为 @XmlElement 并将值字段分配给 Key 字段?


这是我的源文件。

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

请同时找到适配器类:

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

这是具有 HashMap 字段的类:

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


}


请告知如何实现这一目标。

问候,
-阿南德

目前它产生以下输出:

最佳答案

我有同样的要求“我需要 Key 是元素本身,HashMap 的值是元素的值”。

我没有使用自定义适配器,而是通过将 HashMap 条目动态转换为 JAXBElement 对象列表,然后用 @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);
}
}

输出是,

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

关于java - JAXB HashMap 不可映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6820092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com