我正在使用以下代码使用 JAXB 创建 XML,但是在创建 XML 时不包含 XML 声明。
代码:
ServletContext ctx = getServletContext();
String filePath = ctx.getRealPath("/xml/"+username + ".xml");
File file = new File(filePath);
JAXBContext context= JAXBContext.newInstance("com.q1labs.qa.xmlgenerator.model.generatedxmlclasses");
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
OutputStream os = new FileOutputStream(file);
jaxbMarshaller.marshal(test, os);
response.setContentType("text/plain");
response.setHeader("Content-Disposition",
"attachment;filename=xmlTest.xml");
InputStream is = ctx.getResourceAsStream("/xml/"+username + ".xml");
XML 声明:
<?xml version="1.0" encoding="ISO-8859-1"?>
如何让它输出 XML 声明?