通过XmlWriter写入XML
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过XmlWriter写入XML相关的知识,希望对你有一定的参考价值。
包路径:(需要导入包:dom4j-1.6.1.jar和jaxen-1.2.0-atlassian-2.jar)
EMP.java代码内容
1 package xmlDemo; 2 3 public class EMP { 4 int id; 5 String name; 6 int age; 7 String gender; 8 double salary; 9 10 public EMP(int id, String name, int age, String gender, double salary) { 11 this.id = id; 12 this.name = name; 13 this.age = age; 14 this.gender = gender; 15 this.salary = salary; 16 } 17 18 }
XmlWriter.java代码内容
1 package xmlDemo; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.dom4j.Document; 11 import org.dom4j.DocumentHelper; 12 import org.dom4j.Element; 13 import org.dom4j.io.OutputFormat; 14 import org.dom4j.io.XMLWriter; 15 16 public class XmlWriter { 17 public static void main(String[] args) { 18 List<EMP> list = new ArrayList<EMP>(); 19 list.add(new EMP(1, "张三", 23, "男", 20000)); 20 list.add(new EMP(2, "李四", 24, "男", 21000)); 21 list.add(new EMP(3, "王五", 25, "男", 22000)); 22 list.add(new EMP(4, "张三", 26, "男", 23000)); 23 list.add(new EMP(5, "张三", 27, "男", 24000)); 24 // 创建文档对象 25 Document doc = DocumentHelper.createDocument(); 26 // 创建根节点 27 Element root = doc.addElement("emps"); 28 for (EMP emp : list) { 29 // 根据集合里面的emp添加emp节点 30 Element empElement = root.addElement("emp"); 31 // 添加员工的属性节点,并为属性节点=附文本值 32 empElement.addElement("id").setText(Integer.toString(emp.id)); 33 empElement.addElement("age").setText(Integer.toString(emp.age)); 34 empElement.addElement("name").setText(emp.name); 35 empElement.addElement("gender").setText(emp.gender); 36 empElement.addElement("salary").setText(Double.toString(emp.salary)); 37 // 把id当属性添加 38 empElement.addAttribute("id", Integer.toString(emp.id)); 39 } 40 // 设置XML文档格式 41 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); 42 // 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1 43 outputFormat.setEncoding("UTF-8"); 44 // outputFormat.setSuppressDeclaration(true); //是否生产xml头 45 outputFormat.setIndent(true); // 设置是否缩进 46 outputFormat.setIndent(" "); // 以四个空格方式实现缩进 47 outputFormat.setNewlines(true); // 设置是否换行 48 XMLWriter writer = null; 49 try { 50 writer = new XMLWriter(outputFormat); 51 } catch (UnsupportedEncodingException e1) { 52 e1.printStackTrace(); 53 } 54 try { 55 FileOutputStream fos = new FileOutputStream("emps.xml"); 56 writer.setOutputStream(fos); 57 writer.write(doc); 58 writer.close(); 59 System.out.println("写入完毕!"); 60 } catch (UnsupportedEncodingException e) { 61 e.printStackTrace(); 62 } catch (FileNotFoundException e) { 63 e.printStackTrace(); 64 } catch (IOException e) { 65 e.printStackTrace(); 66 } 67 } 68 }
写入完成的emps.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <emps> 4 <emp id="1"> 5 <id>1</id> 6 <age>23</age> 7 <name>张三</name> 8 <gender>男</gender> 9 <salary>20000.0</salary> 10 </emp> 11 <emp id="2"> 12 <id>2</id> 13 <age>24</age> 14 <name>李四</name> 15 <gender>男</gender> 16 <salary>21000.0</salary> 17 </emp> 18 <emp id="3"> 19 <id>3</id> 20 <age>25</age> 21 <name>王五</name> 22 <gender>男</gender> 23 <salary>22000.0</salary> 24 </emp> 25 <emp id="4"> 26 <id>4</id> 27 <age>26</age> 28 <name>张三</name> 29 <gender>男</gender> 30 <salary>23000.0</salary> 31 </emp> 32 <emp id="5"> 33 <id>5</id> 34 <age>27</age> 35 <name>张三</name> 36 <gender>男</gender> 37 <salary>24000.0</salary> 38 </emp> 39 </emps>
以上是关于通过XmlWriter写入XML的主要内容,如果未能解决你的问题,请参考以下文章
如何将 EntityReference(例如 )写入 XmlNodeReader 读取的 XmlWriter?
通过 System.Xml.XmlWriter 取消转义尖括号