字符串生成xml文件

来源:互联网 发布:什么软件开发最有前途 编辑:程序博客网 时间:2024/06/05 03:37
/**
* 字符串,生成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());
  }
}
原创粉丝点击