JAXB生成XML文件去除Standlone
/**
* JAXB:java对象转换为xml文件(java對象需要添加註解)
* @param obj 要序列化文件的java对象
* @param model java对象.Class
* @param path 生成的xml文件路径
* @return boolean 是否成功
* @throws Exception
*/
public static boolean beanToXmlByJAXB(Object obj,Class<?> model,String path){
try{
JAXBContext context = JAXBContext.newInstance(model);
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n ");
//获得Marshaller对象
Marshaller marshaller = context.createMarshaller();
//格式化xml格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//去掉生成xml的默认报文头
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
//java 对象生成到writer
marshaller.marshal(obj,writer);
//转化为String类型
String stringXML = writer.toString();
FileWriter fWriter=new FileWriter(new File(path));
fWriter.write(stringXML);
fWriter.close();
}catch(Exception e){
return false;
}
return true;
}