Jaxb complex xml unmarshall [英] Jaxb complex xml unmarshall
问题描述
我遇到了解决嵌套xml问题的问题。有人可以告诉我,如果我遗失了什么。
正文
标签可以包含任何Jaxb anotated obj。
我是否必须创建一个自定义适配器,用于编组/解组这样的xml?
I am having issues to unmarshall nested xml below. Can someone please advise if I am missing something.
body
tag can contain any Jaxb anotated obj.
Do I have to create a custom adapter for marshalling/unmarshalling such xml?
输入XML
<?xml version="1.0" encoding="UTF-8"?>
<serviceRq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="serviceRq">
<body>
<createRq>
<id>1234</id>
</createRq>
</body>
</serviceRq>
我的Jaxb注释类是:
My Jaxb-annotated classes are:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "serviceRq")
public class ServiceRq{
private Object body;
<!-- getters and setters omitted-->
}
这里,body可以是任何jaxb注释对象,在本例中是CreateRq。
Here, body can be any jaxb annotated object, in this case its CreateRq.
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "createRq")
public class CreateRq{
private String id;
<!-- getters and setters omitted-->
}
我正在寻找支持体内任何Jaxb注释对象的通用方法输入xml。
I am looking for a generic way to support any Jaxb annotated object in body of the input xml.
推荐答案
你可以使用 @XmlAnyElement(lax = true)
和一个 XmlAdapter
来处理这个用例:
You could use a @XmlAnyElement(lax=true)
and an XmlAdapter
to handle this use case:
ServiceRq
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "serviceRq")
public class ServiceRq{
@XmlJavaTypeAdapter(value=BodyAdapter.class)
private Object body;
// getters and setters omitted
}
BodyAdapter
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class BodyAdapter extends XmlAdapter<Body, Object>{
@Override
public Object unmarshal(Body v) throws Exception {
return v.getValue();
}
@Override
public Body marshal(Object v) throws Exception {
Body body = new Body();
body.setValue(v);
return body;
}
}
身体
import javax.xml.bind.annotation.XmlAnyElement;
public class Body {
private Object value;
@XmlAnyElement(lax=true)
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
CreateRq
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "createRq")
public class CreateRq{
private String id;
// getters and setters omitted
}
演示
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ServiceRq.class);
System.out.println(jc);
Unmarshaller unmarshaller = jc.createUnmarshaller();
ServiceRq serviceRq = (ServiceRq) unmarshaller.unmarshal(new File("input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(serviceRq, System.out);
}
}
更多信息
- http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html
- http://bdoughan.blogspot.com /2010/07/xmladapter-jaxbs-secret-weapon.html
- http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
- http://bdoughan.blogspot.com/2010 /12/represent-string-values-as-element.html
- http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html
- http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
- http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
- http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html
这篇关于Jaxb complex xml unmarshall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!