Need help in formatting JAXB output

后端 未结 2 1545
我在风中等你
我在风中等你 2021-01-13 16:54

I have some objects let\'s say two, A and B. These objects from the same class. I need to marshal these objects using JAXB and the output XML should be in this form:

2条回答
  •  北海茫月
    2021-01-13 17:33

    Try this:

    import java.io.StringWriter;
    import javax.xml.bind.Marshaller;
    
    ...
    
    Object requestObject = ...  // This is the object that needs to be printed with indentation
    Marshaller marshaller = ...
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter stringWriter = new StringWriter();
    marshaller.marshal(requestObject, stringWriter);
    
    System.out.println(stringWriter.toString());
    

提交回复
热议问题