使用JAXB进行多态XML绑定 [英] Polymorphic XML Binding with JAXB

查看:160
本文介绍了使用JAXB进行多态XML绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我要做的事情:

XML就是这样:

<Doctype1>
    <Outter>
        <Inner>
          <ProblemType>
              <foo>...</foo>
              <baz>...</baz>
          </ProblemType>
        </Inner>
    </Outter>
</Doctype1>

< - 但是,我有: - >

<-- However, I have: -->

<Doctype2>
    <Outter>
        <Inner>
          <ProblemType>
              <blah>...</blah>
              <whatever>...</whatever>
          </ProblemType>
        </Inner>
    </Outter>
</Doctype1>


分享大部分相同的字段,但在Doctype1的情况下,我需要ProblemType1在Doctype2中我需要ProblemType2。

and Share most of the same fields, but in the case of Doctype1 I need ProblemType1 and in Doctype2 I need ProblemType2.

我希望能够重用我绑定的类,因为它们是所有doc类型的通用标记。

I want to be able to reuse the classes I bind to for and as they are a common tag across all doc types.

@XmlAccessorType(XmlAccessType.FIELD)
public class Doc1{

    @XmlElement(name = "Outter")
    public List<Outter> outtards;

}

@XmlAccessorType(XmlAccessType.FIELD)
public class Outter {

    @XmlElement(name = "Inner")
    public List<Innard> innards;
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Innard{

    // need to change this if it's Doc1 or Doc2. 
    //The subtype of problem type needs to change based on DocX
    // the element type name won't change
    @XmlElement(name = "Inner")
    public ProblemType subtype; 
}

似乎也许工厂正常运转?

It seems like Maybe a factory is in order?

@XmlType(factoryClass= , factoryMethod=) 


推荐答案

  @XmlAccessorType(XmlAccessType.NONE)
    public class Doc1 {

        @XmlElement(type = Outter1.class, name = "Outter")
        private List<Outter> outters;

        public static class Outter1 extends Outter {

            @Override
            @XmlElement(type = Inner1.class, name = "Inner")
            public List<Inner> getInner() {
                return super.getInner();
            }

            @Override
            public void setInner(List<Inner> innards) {
                super.setInner(innards);
            }

            public static class Inner1 extends Inner<ProblemType1> {

                @Override
                @XmlElement(type = ProblemType1.class, name = "ProblemType")
                public List<ProblemType> getProblemTypes() {
                    return super.getProblemTypes();
                }

                @Override
                public void setProblemTypes(List<ProblemType> problemTypes) {
                    super.setProblemTypes(problemTypes);
                }
            }
        }
    }

其他class

public class Doc2 {

    @XmlElement(type = Outter2.class, name= "Outter")
    private List<Outter> outters;

    public static class Outter2 extends Outter {

        @Override
        @XmlElement(type = Outter2.class, name = "Inner")
        public List<Inner> getInner() {
            return super.getInner();
        }

        @Override
        public void setInner(List<Inner> innards) {
            super.setInner(groups);
        }

        public static class Inner1 extends Inner<ProblemType2> {
            @Override
            @XmlElement(type = ProblemType2.class, name = "ProblemType")
            public List<ProblemType> getProblemTypes() {
                return super.getProblemTypes();
            }

            @Override
            public void setProblemTypes(List<ProblemType> transactions) {
                super.setProblemTypes(transactions);
            }
        }
    }
}

I我花了一些时间试图减少它,但它似乎没有响应XmlAccesorType.FIELD。如果我使用与超级相同的属性名称,则无关紧要。

I have spent some time trying to reduce it but it doesn't seem to respond to XmlAccesorType.FIELD. If I use the same property name as the super or not it doesn't matter.

这篇关于使用JAXB进行多态XML绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆