У меня есть реализация класса XmlAdapter следующим образом:
public class XMLSimpleElementAdapter extends XmlAdapter<Element, String> {
protected static final DocumentImpl doc=new DocumentImpl(true);
protected String elementName;
protected String nameSpace;
public XMLSimpleElementAdapter() {
this("Description","https://www.proviseconsulting.com/ProcessDataModelDefinition");
}
public XMLSimpleElementAdapter(String elementName, String nameSpace) {
this.elementName=elementName;
this.nameSpace=nameSpace;
}//constructor closing
@Override
public Element marshal(String value) throws Exception {
System.out.println("XMLSimpleElementAdapter -- marshal called");
Element element=doc.createElement(elementName);
element.setAttribute("xmlns", nameSpace);
if(value!=null) {
Node textValue=doc.createTextNode(value);
element.appendChild(textValue);
}//if closing
return (Element)element;
}//marshal closing
@Override
public String unmarshal(Element element) throws Exception {
System.out.println("XMLSimpleElementAdapter -- unmarshal called");
String value="Some hard coded value";
try {
System.out.println("Element = "+element.getParentNode());
}catch(Exception e) {e.printStackTrace();}
Node textValue=element.getFirstChild();
if(textValue!=null) {value=textValue.getNodeValue();}
return value;
}//unmarshal closing
}//class closing
Я использую адаптер для атрибута класса:
@XmlAnyElement
//@XmlElement(name="Description",namespace="https://www.proviseconsulting.com/TriggerConfig")
@XmlJavaTypeAdapter(value = XMLSimpleElementAdapter.class)
protected String description;
Где XML: <Description>This is the description of the configuration.</Description>
Работает отлично, и я получаю значение.Однако, если я использую закомментированную аннотацию @XmlElement вместо @XmlAnyElement, я получаю следующую ошибку:
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
org.w3c.dom.Element is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at org.w3c.dom.Element
at protected java.lang.String com.provise.wfm.trigger.config.TriggerConfigImpl.description
at com.provise.wfm.trigger.config.TriggerConfigImpl
Я что-то упустил, JAXB может обработать org.W3c.dom.Element с @XmlAnyTypeа не с типом @XmlElement ??
Я использую реализацию JAXB из Sun / Oracle с JDK 12 (AdoptOpenJDK с J9)