我有以下代码:
public static void main(String[] args) throws IOException, JAXBException
{
Shop shop=new Shop();
shop.names.add("S1");
shop.names.add("S2");
shop.count=12;
shop.profit=123.4;
shop.secretData.add("String1");
shop.secretData.add("String2");
shop.secretData.add("String3");
shop.secretData.add("String4");
shop.secretData.add("String5");
StringWriter writer = new StringWriter();
convertToXml(writer, shop);
System.out.println(writer.toString());
//toXmlWithComment(shop, "second", "it's a comment");
}
public static void convertToXml(StringWriter writer, Object obj) throws IOException, JAXBException
{
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(obj, writer);
}
也许我需要使用Marshaller的一种方法,但我现在不需要。 如何插入评论?