Dom4j生成xml
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateXML {
public static Document getDocument(){
Document document = DocumentHelper.createDocument();
//生成一个接点
Element root = document.addElement("resinfo");
root.addAttribute("ver", "1.0");
//生成root的一个接点
Element resid = root.addElement("resid");
resid.addAttribute("caption","作品编号").addText("102102102");
Element filename = root.addElement("filename");
filename.addAttribute("caption","文件名称").addText("文件名称");
Element support_documents = root.addElement("support_documents");
support_documents.addAttribute("caption","其它文件");
Element filename1 = support_documents.addElement("filename");
filename1.addAttribute("caption","文件名称");
Element title = root.addElement("title");
title.addAttribute("caption","资源名称");
title.addComment("注释 注释");
Element stage = root.addElement("stage");
stage.addComment("注释 注释");
stage.addAttribute("caption","学段").setText("小学一年级");
return document;
}
/**
* 写入xml文件地址
* @param document 所属要写入的内容
* @param outFile 文件存放的地址
*/
public static void writeDocument(Document document, String outFile){
try{
//读取文件
//FileWriter fileWriter = new FileWriter(outFile);
FileOutputStream fileWriter = new FileOutputStream(outFile); //如果有中文,用这个
//设置文件编码
OutputFormat xmlFormat = OutputFormat.createPrettyPrint(); //格式化输出
xmlFormat.setEncoding("UTF-8");
//创建写文件方法
XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat);
//写入文件
xmlWriter.write(document);
//关闭
xmlWriter.close();
}catch(IOException e){
System.out.println("文件没有找到");
e.printStackTrace();
}
}
public static void main(String[] args){
CreateXML.writeDocument(CreateXML.getDocument(), "D:/xmltest.xml");
}
}
Jdom输出xml
public void BuildXMLDoc() throws IOException, JDOMException {
// 创建根节点 list;
Element root = new Element("list");
// 根节点添加到文档中;
Document Doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
for (int i = 0; i < 5; i++) {
// 创建节点 user;
Element elements = new Element("user");
// 给 user 节点添加属性 id;
elements.setAttribute("id", "" + i);
// 给 user 节点添加子节点并赋值;
// new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值;
elements.addContent(new Element("name").setText("xuehui"));
elements.addContent(new Element("age").setText("28大幅度"));
elements.addContent(new Element("sex").setText("Male"));
// 给父节点list添加user子节点;
root.addContent(elements);
}
XMLOutputter XMLOut = new XMLOutputter();
// 输出 user.xml 文件;
XMLOut.output(Doc, new FileOutputStream("user.xml"));
}
public static void main(String[] args) {
try {
Java2XML j2x = new Java2XML();
System.out.println("生成 mxl 文件...");
j2x.BuildXMLDoc();
} catch (Exception e) {
e.printStackTrace();
}
}
使用DOM4J的XMLWriter输出UTF-8编码的XML文件时,出现乱码。
首先,设置输出的编码,在这我们使用“utf-8”
Java代码
1.OutputFormat format = OutputFormat.createPrettyPrint();
2. format.setEncoding("utf-8");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
输出代码:
Java代码
1.try {
2. output = new XMLWriter(new FileWriter("entity.xml"), format);
3. output.write(document);
4. output.close();
5. } catch (IOException e) {
6. e.printStackTrace();
7. }
try {
output = new XMLWriter(new FileWriter("entity.xml"), format);
output.write(document);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
上面的输出如果有中文,可以会出现乱码的问题,将上面的FileWriter改成FileOutputStream便可以了。
Java代码
1.try {
2. output = new XMLWriter(new FileOutputStream("entity.xml"), format);
3. output.write(document);
4. output.close();
5. } catch (IOException e) {
6. e.printStackTrace();
7. }
try {
output = new XMLWriter(new FileOutputStream("entity.xml"), format);
output.write(document);
output.close();
} catch (IOException e) {
e.printStackTrace();
}