Java 包含映射或简单类型的JAXB对象 包含映射或简单类型的JAXB对象
其思想是让一个类具有一个成员“value”,该成员可以包含简单类型(如String或Integer)以及一个简单类型的映射,该映射连接到一个布尔值(如HashMap) 下面是一个“工作”示例,它显示了我想要做的事情:Java 包含映射或简单类型的JAXB对象 包含映射或简单类型的JAXB对象,java,object,map,jaxb,hashmap,Java,Object,Map,Jaxb,Hashmap,其思想是让一个类具有一个成员“value”,该成员可以包含简单类型(如String或Integer)以及一个简单类型的映射,该映射连接到一个布尔值(如HashMap) 下面是一个“工作”示例,它显示了我想要做的事情: package test; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXB; import javax.xml.bind.annota
package test;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement
public class TestJAXB {
public static void main(String[] args) {
TestJAXB test = new TestJAXB();
JAXB.marshal(test, new File("foo.xml"));
}
Container one;
Container two;
Container three;
Container four;
public TestJAXB() {
this.one = new Container("foo");
this.two = new Container("42");
this.three = new Container(true);
HashMap<Object, Boolean> map = new HashMap<Object, Boolean>();
map.put("foo", false);
map.put("bar", false);
map.put("foobar", true);
this.four = new Container(map);
}
@XmlElement
public Container getOne() {
return one;
}
public void setOne(Container one) {
this.one = one;
}
@XmlElement
public Container getTwo() {
return two;
}
public void setTwo(Container two) {
this.two = two;
}
@XmlElement
public Container getThree() {
return three;
}
public void setThree(Container three) {
this.three = three;
}
@XmlElement
public Container getFour() {
return four;
}
public void setFour(Container four) {
this.four = four;
}
}
@XmlRootElement
@XmlSeeAlso(HashMap.class)
class Container {
Map<Object, Boolean> value = null;
boolean wasMap = false;
protected Container() {
}
protected Container(Object value) {
this.setValue(value);
}
@XmlElements({ @XmlElement(name = "string", type = String.class), @XmlElement(name = "bool", type = Boolean.class), @XmlElement(name = "int", type = Integer.class), })
public Object getValue() {
if (wasMap) {
return value;
} else {
return value.keySet().toArray()[0];
}
}
@SuppressWarnings("unchecked")
public void setValue(Object value) {
if (value instanceof Map) {
this.value = (Map<Object, Boolean>) value;
this.wasMap = true;
} else {
this.value = new HashMap<Object, Boolean>();
this.value.put(value, false);
this.wasMap = false;
}
}
}
封装测试;
导入java.io.File;
导入java.util.HashMap;
导入java.util.Map;
导入javax.xml.bind.JAXB;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlElements;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.xmlsee;
@XmlRootElement
公共类TestJAXB{
公共静态void main(字符串[]args){
TestJAXB test=newtestjaxb();
marshal(测试,新文件(“foo.xml”);
}
集装箱一号;
二号货柜;
三号货柜;
四号货柜;
公共TestJAXB(){
this.one=新容器(“foo”);
本条第2款=新集装箱(“42”);
this.three=新容器(true);
HashMap=newHashMap();
map.put(“foo”,false);
地图。放置(“条”,假);
map.put(“foobar”,true);
this.four=新容器(地图);
}
@XmlElement
公共容器getOne(){
返回一个;
}
公共空间集合一(容器一){
这个1=1;
}
@XmlElement
公共容器getTwo(){
返回两个;
}
公共空间集合二(容器二){
这个2=2;
}
@XmlElement
公共容器getThree(){
返回三个;
}
公共空间集合三(容器三){
这三个=三个;
}
@XmlElement
公共容器getFour(){
返回四个;
}
公共空间集合4(容器4){
这个4=4;
}
}
@XmlRootElement
@XMLSeeAllow(HashMap.class)
类容器{
映射值=空;
布尔wasMap=false;
受保护容器(){
}
受保护容器(对象值){
这个.setValue(值);
}
@XmlElements({@XmlElement(name=“string”,type=string.class),@XmlElement(name=“bool”,type=Boolean.class),@XmlElement(name=“int”,type=Integer.class),})
公共对象getValue(){
如果(wasMap){
返回值;
}否则{
返回值.keySet().toArray()[0];
}
}
@抑制警告(“未选中”)
公共无效设置值(对象值){
if(映射的值实例){
this.value=(Map)值;
this.wasMap=true;
}否则{
this.value=new HashMap();
this.value.put(value,false);
this.wasMap=false;
}
}
}
简单类型被编组为精细xml,但映射的元素保持为空:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testJAXB>
<four>
<string xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="hashMap"/>
</four>
<one>
<string>foo</string>
</one>
<three>
<bool>true</bool>
</three>
<two>
<string>42</string>
</two>
</testJAXB>
福
真的
42
我做错了什么?我能做什么?您必须为HashMap创建
javax.xml.bind.annotation.adapters.XmlAdapter
。您可以在这里获得的示例
必须为HashMap创建
javax.xml.bind.annotation.adapters.XmlAdapter
。您可以在这里获得的示例
这解决了我的问题-部分我仍然需要摆弄访问作为“正常”变量或映射的值。非常感谢!这解决了我的问题-部分我仍然需要摆弄访问作为“正常”变量或映射的值。非常感谢!