dom4j将生成好的doc写入磁盘,并解决中文乱码
2015-01-01 16:29
267 查看
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);
}
}
相关文章推荐
- dom4j 生成xml windows 是GB2312中文正常,linux是乱码解决方法
- 解决使用eclipse生成doc时的中文乱码问题
- php在线生成pdf中文乱码完美解决``
- JFreeReport生成 html、pdf文件中文乱码的解决办法
- DOM4J 生成中文XML出现乱码
- C#写入Oracle 中文乱码的解决办法
- php在线生成pdf中文乱码完美解决``
- JFreeChart生成折线图(生成图片本地保存,有解决中文乱码的通用方法)
- 使用mule生成WSDL中文乱码问题解决方法
- PowerDesigner生成MySQL数据库中文乱码问题解决
- jdom解决中文乱码问题 JAVA生成xml文件
- c# 中StreamWriter写入中文出现乱码的解决办法
- doc 下解决mysql 中文乱码问题
- CodeSmith 3.2生成中文时乱码的临时解决
- mysql 读出及写入中文乱码问题与解决
- Antenna 打包 j2me 生成 JAD和MANIFEST.MF中文乱码的另一种解决方法
- iText生成PDF文件(解决了中文乱码)
- Java IO 读取/写入文件 中文乱码困难解决代码
- 解决VS2008生成的文件在XCode下中文乱码
- Java中按照行读取和写入文件 日志常用操作 解决中文乱码问题