字符串生成xml文件
/**
* 字符串,生成xml文件
* @param str
* @param fileName
* @throws IOException
*/
public void strChangeXML(String str,String fileName ) throws IOException {
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes()));
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码 */
format.setEncoding("UTF-8");
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(fileName)),format);
writer.write(document);
writer.close();
} catch (DocumentException e) {
// TODOAuto-generatedcatchblock
logger.error(e.getMessage());
}
}
欢迎关注公众号:Java后端技术全栈