merge multiple files with JAXB

帅比萌擦擦* 提交于 2021-02-17 05:55:09

问题


It's the first time I'm using stackoverflow and I don't speak English perfectly so be nice please.

I'm using Jaxb in append mode like that

for (Document330 document : documents){
  JAXBContext jContext = JAXBContext.newInstance(Document330Xml.class);
    Marshaller m = jContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(document, fos);
}

And I have an output file like that:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
    <MAILING>
        <REF>M584</REF>
        <LIBELLE>Mail Test 1</LIBELLE>
    </MAILING>
</DOCUMENT>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
    <MAILING>
        <REF>M585</REF>
        <LIBELLE>Mail Test 2</LIBELLE>
    </MAILING>
</DOCUMENT>

but I want something like that :

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENTS>
    <DOCUMENT>
        <MAILING>
            <REF>M584</REF>
            <LIBELLE>Mail Test 1</LIBELLE>
        </MAILING>
    </DOCUMENT>
    <DOCUMENT>
        <MAILING>
            <REF>M585</REF>
            <LIBELLE>Mail Test 2</LIBELLE>
        </MAILING>
    </DOCUMENT>
</DOCUMENTS>

But it is possible that I have many XML. So I do not think the Unmarshaller is the best solution

Thanks for reading me


回答1:


If I remind correctly, you need to create a Documents330Xml class, which can be marshalled (you can have a look at your Document330Xml class for reference). This class needs a list of Document330Xml as field.

If you then marshall the Documents330Xml class, you should get the desired XML.



来源:https://stackoverflow.com/questions/49858160/merge-multiple-files-with-jaxb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!