我有一个带有根元素的XML格式" Blurb"其中包含一组名为namespace的子元素,每个元素都有一个必需的属性" name"。我用xjc构建了一些类,它创建了一个包含Namespace对象列表的Blurb类。
使用命名空间名称作为键的Hash和作为值的Namespace对象对我来说似乎更方便,我开始制作XMLAdapter。使用this blog entry作为我的向导,我创建了一个适配器类并将其链接。它构建,但是当我运行它时,我得到错误," java.lang.AssertionError:1个IllegalAnnotationExceptions"的计数。我已经查找了XmlJavaTypeAdapter的不同用法,但它们看起来并不一致。我已经尝试将注释放在属性,方法和两者上,并且都给了我类似的错误。有人能告诉我我做错了吗?
这是我的主要课程:
package com.jolyjonesfamily.blurb.models;
import com.jolyjonesfamily.blurb.map.adapter.BlurbMapAdapter;
import java.util.HashMap;
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.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"pattern",
"namespace"
})
@XmlRootElement(name = "blurb")
public class Blurb {
@XmlElement(required = true)
protected Cat pattern;
@XmlJavaTypeAdapter(BlurbMapAdapter.class)
public Map<String, Namespace> namespace;
// @XmlJavaTypeAdapter(BlurbMapAdapter.class)
public Map<String, Namespace> getNamespace() {
if (namespace == null) {
namespace = new HashMap<String, Namespace>();
}
return this.namespace;
}
public void setNamespace(Map<String, Namespace> namespaceMap) {
namespace = namespaceMap;
}
public Cat getPattern() {
return pattern;
}
public void setPattern(Cat value) {
this.pattern = value;
}
}
这是我的适配器:
package com.jolyjonesfamily.blurb.map.adapter;
import com.jolyjonesfamily.blurb.models.Namespace;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by samjones on 10/19/15.
*/
public class BlurbMapAdapter extends XmlAdapter<List<Namespace>, Map<String, Namespace>> {
@Override
public Map<String, Namespace> unmarshal(List<Namespace> namespaceList) throws Exception {
Map<String, Namespace> namespaceMap = new HashMap<String, Namespace>();
for(Namespace myNamespace : namespaceList) {
namespaceMap.put(myNamespace.getName(), myNamespace);
}
return namespaceMap;
}
@Override
public List<Namespace> marshal(Map<String, Namespace> namespaceMap) throws Exception {
List<Namespace> result = new ArrayList<Namespace>();
result.addAll(namespaceMap.values());
return result;
}
}
如果它很重要,请点击我的命名空间类:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2015.10.07 at 09:29:23 PM CDT
//
package com.jolyjonesfamily.blurb.models;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}category" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"category"
})
@XmlRootElement(name = "namespace")
public class Namespace {
protected List<Category> category;
@XmlAttribute(name = "name", required = true)
@XmlSchemaType(name = "anySimpleType")
protected String name;
/**
* Gets the value of the category property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the category property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getCategory().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Category }
*
*
*/
public List<Category> getCategory() {
if (category == null) {
category = new ArrayList<Category>();
}
return this.category;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}