TL;博士
只要您将值类型指定为(not ) ,您就可以XmlAdapter
将域对象转换为实例。org.w3c.dom.Element
Object
Element
下面是一个完整的例子。
xml适配器
类型的字段/属性java.lang.Object
会将未知内容保留为 DOM 节点。XmlAdapter
您可以通过在as中指定值类型来在您的用例中利用这一点Object
。您需要确保从该方法返回的根元素与注释marshal
定义的字段/属性相匹配。@XmlElement
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class BarAdapter extends XmlAdapter<Object, Bar>{
private DocumentBuilder documentBuilder;
public BarAdapter() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
documentBuilder = dbf.newDocumentBuilder();
} catch(Exception e) {
// TODO - Handle Exception
}
}
@Override
public Bar unmarshal(Object v) throws Exception {
Bar bar = new Bar();
Element element = (Element) v;
bar.value = element.getTextContent();
return bar;
}
@Override
public Object marshal(Bar v) throws Exception {
Document document = documentBuilder.newDocument();
Element root = document.createElement("bar");
root.setTextContent(v.value);
return root;
}
}
Java 模型
富
@XmlJavaTypeAdapter
注释用于XmlAdapter
引用.
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlJavaTypeAdapter(BarAdapter.class)
private Bar bar;
}
酒吧
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
String value;
}
演示代码
演示
由于创建 DocumentBuilderFactory 是有成本的,我们可以通过在 Marshaller 上设置一个实例来利用 JAXB 处理 XmlAdapter 的有状态实例的能力。
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum18272059/input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setAdapter(new BarAdapter());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar>Hello World</bar>
</foo>