使用JAXB和Any进行序列化(不使用ElementNS)

时间:2015-07-20 13:25:46

标签: jaxb

我正在寻找最好的做法来编组一个可以处理String,Long等的XMLAnyElement ......我找到了Serializing with JAXB and the Any,但是我需要避免使用ElementNS并解决附加的案例

DOMHandler是最好的方式吗?

public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(Payload.class, Foo.class, ObjectFactory.class);

    Payload payload = new Payload();
    payload.any = new ArrayList<>();
    payload.any.add(new Bar());
    payload.any.add(new Foo());
    payload.any.add("pepe"); 

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(payload, System.out);
}

1 个答案:

答案 0 :(得分:0)

已解决,请参阅github上的我的PoC ..

https://github.com/franciscophilip/jaxb-payload-poc/