Add XML comments into marshaled file

前端 未结 3 1764
太阳男子
太阳男子 2020-12-20 12:31

I\'m marshaling objects into an XML file. How can I add comments into that XML file?

3条回答
  •  既然无缘
    2020-12-20 12:58

    You can add comments right after the preamble with the proprietary Marshaller property com.sun.xml.bind.xmlHeaders (see XML Preamble Control)

    In the included JAXB-implementation jdk1.6.0_29 the property is called "com.sun.xml.internal.bind.xmlHeaders"

    See also question: How to add DOCTYPE and xml processing instructions when marshalling with JAXB?

    So to get this XML with the test-comment after the preamble:

    
    
    
        Daniel
        1982-06-09T00:00:00+02:00
    
    

    You can use this Java-Code:

    JAXBContext context = JAXBContext.newInstance(Player.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty("com.sun.xml.internal.bind.xmlHeaders", "\n");
    m.marshal(player, System.out);
    

提交回复
热议问题