Jaxb 复杂 xml 解组

2023-12-25

我在解组下面的嵌套 xml 时遇到问题。如果我遗漏了什么,有人可以告诉我吗?
body标签可以包含任何 Jaxb 带注释的 obj。
我是否必须创建一个自定义适配器来编组/解组此类 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 注释类是:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "serviceRq")
public class ServiceRq{    
    private Object body;
    <!-- getters and setters omitted-->
}

这里,body 可以是任何 jaxb 带注释的对象,在本例中是 CreateRq。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "createRq")
public class CreateRq{    
    private String id;
    <!-- getters and setters omitted-->
}

我正在寻找一种通用方法来支持输入 xml 正文中的任何 Jaxb 注释对象。


你可以使用@XmlAnyElement(lax=true) and an XmlAdapter处理这个用例:

服务请求

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
}

身体适配器

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;
    }

}

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
}

Demo

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/08/using-xmlanyelement-to-build-generic.html
  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.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/jaxb-and-immutable-objects.html
  • http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jaxb 复杂 xml 解组 的相关文章

随机推荐