dom4j将生成好的doc写入磁盘,并解决中文乱码
来源:互联网 发布:淘宝站外活动销量 编辑:程序博客网 时间:2024/06/10 14:52
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class XmlWriter {
private void writeDocument(Document document, String dest) {
XMLWriter xmlWriter = null;
try {
PrintWriter printWriter = new PrintWriter(dest, "UTF-8");
OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
xmlWriter = new XMLWriter(printWriter, xmlFormat);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != xmlWriter) {
xmlWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
root.addElement("child").setText("i am a child");
String dest = "C:"+File.separator+"temp"+File.separator+"世界是我们的.xml";
System.out.println(dest);
File file = new File(dest);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
XmlWriter xw = new XmlWriter();
xw.writeDocument(doc, dest);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class XmlWriter {
private void writeDocument(Document document, String dest) {
XMLWriter xmlWriter = null;
try {
PrintWriter printWriter = new PrintWriter(dest, "UTF-8");
OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
xmlWriter = new XMLWriter(printWriter, xmlFormat);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != xmlWriter) {
xmlWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
root.addElement("child").setText("i am a child");
String dest = "C:"+File.separator+"temp"+File.separator+"世界是我们的.xml";
System.out.println(dest);
File file = new File(dest);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
XmlWriter xw = new XmlWriter();
xw.writeDocument(doc, dest);
}
}
0 0
- dom4j将生成好的doc写入磁盘,并解决中文乱码
- 解决使用eclipse生成doc时的中文乱码问题
- dom4j生成xml文件的中文乱码
- dom4j生成xml文件的中文乱码
- dom4j 修改xml并写入磁盘
- eclipse生成doc文档及乱码的解决
- DOM4J生成XML文件的中文乱码问题
- 解决js 写入中文乱码
- doc 下解决mysql 中文乱码问题
- 使用commons-fileupload实现文件(doc、pdf)上传,提取表单参数并解决中文乱码
- DOM4J的中文乱码问题
- Java Dom4j 解决中文乱码问题
- Python 2.7 解决写入文件的中文乱码问题
- 解决MySQL中写入中文出现乱码的问题
- dom4j 生成xml 并将其格式化
- 解决hibernate写入mysql中文乱码问题
- python中文写入CSV乱码解决
- 将变量写入磁盘文件的方法
- Java基础----System.in、System.out
- 使用 FIFO 实现进程间通信示例
- 需求:修改指定目录内所有含有某字段文件
- Android内核详解之Low memory killer
- Django引用CSS示例
- dom4j将生成好的doc写入磁盘,并解决中文乱码
- Ubuntu 搭建PHP环境
- 文件目录操作一----------文件复制
- Android Framework 记录
- 黑马程序员——NSArray的基本使用和常见方法的使用
- Gvim插件
- 解决图片太大溢出问题、CSS 限制图片最大宽度、图片按比例自动缩放
- LeetCode(162) Find Peak Element
- inifile