首页 文章

Jaxb unmarshall取决于根元素

提问于
浏览
0

我试图解密从休息调用返回到POJO的Xml . 但是,一次调用可以返回具有不同根元素的不同类型的文档,例如

<obj1> ... </obj1>
<obj2> ... </obj2>

我使用泛型函数解组:

private <T> T unmarshal(String xml, Class<T> clazz) {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    return clazz.cast(jc.createUnmarshaller().unmarshal(new StringReader(xml));
}

我已创建为每个不同的根分开类,但我不知道如何检查根元素类型然后调用我的unmarshall函数与正确的类型?

if rootElement.equals("obj1")
    Obj1 obj = unmarshal(xml, Obj1.class)
else if rootElement.equals("obj2")
    Obj2 obj = unmarshal(xml, Obj2.class)

有没有办法使用JaxB对根元素进行这种条件检查?

2 回答

  • 0

    对的,这是可能的 .

    • 使用 @XmlRootElement 声明每个可能的根类 .

    • 使用如下所有可能的根类创建JAXBContext .

    JAXBContext jc = JAXBContext.newInstance(Class...)

    • 然后,

    Object obj = unmarshal(xml); if(obj instanceof Root1) { // cast to Root1 object } else obj instanceof Root2) { // cast to Root2 object }

  • 0

    我不知道是否有更好的方法,但我找不到一个 . 为了解决这个问题,我创建了一个包含两种根元素类型的对象:

    @Data
    public class compositionObject {
    private Obj1 obj1;
    private Obj2 obj2;
    
    public compositionObject(final Object obj) {
        if(obj instanceof Obj1) {
            this.obj1 = obj1;
        } else if(obj instanceof Obj2) {
            this.obj2 = obj2;
        } else {
            throw new IllegalArgumentExcepion("not supported");
        }
    }
    

    以半通用的方式解组:

    private Object unmarshal(String xml, Class<?>... clazzes) {
        JAXBContext jc = JAXBContext.newInstance(clazzes);
        return clazz.cast(jc.createUnmarshaller().unmarshal(new StringReader(xml));
    }
    

    使用 @XmlRegistry@XmlElementDecl 不会给我预期的行为,因为它将返回 JAXBElement<Obj1> 而不是 JAXBElement<CompositionObject> . 以下不起作用:

    private final static QName OBJ1_QNAME = new QName("", "obj1");
    private final static QName COMP_OBJ_QNAME = new QName("", "compositionobj");
    
    @XmlElementDecl(namespace = "",  name = "obj1")
    public JAXBElement<CompositionObject> createObj1(final Obj1 value) {
        final CompositionObject compObj = new CompositionObject();
        comPbj.setObj1(value);
        return new JAXBElement<CompositionObject>(COMP_OBJ_QNAME, CompositionObject.class, null, value);
    }
    

    问题:@XmlRegistry - how does it work?回答为什么不能以这种方式使用@XmlRegistry .

相关问题