百度了一些博客,大同小异,在选取jar包工具的时候大概看了下,大抵是jdom原始,dom4j优秀。于是做了些练习。
参考:http://www.cnblogs.com/mengdd/archive/2013/06/05/3119927.html
1 package com.test.xml; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.List; 7 import java.util.Random; 8 9 import org.dom4j.Document; 10 import org.dom4j.DocumentException; 11 import org.dom4j.DocumentHelper; 12 import org.dom4j.Element; 13 import org.dom4j.io.OutputFormat; 14 import org.dom4j.io.SAXReader; 15 import org.dom4j.io.XMLWriter; 16 17 /** 18 * @ClassName: TestDom4j 19 * @Description:dom4j练习xml的读写 20 * @author mrf 21 * @date 2015-11-11 下午02:25:20 22 * 23 */ 24 public class TestDom4j { 25 26 /** 27 * @Title: read 28 * @Description:读取xml文件 29 * @param @param path 参数 30 * @return void 返回类型 31 * @throws DocumentException 32 */ 33 public static void read(String path) throws DocumentException{ 34 SAXReader saxReader = new SAXReader(); 35 Document doc = saxReader.read(new File(path)); 36 //获取根元素 37 Element root = doc.getRootElement(); 38 System.out.println(root.getName()+root.attributeValue("name")); 39 //获取子元素 40 @SuppressWarnings("unchecked") 41 List<Element> grades = root.elements(); 42 if(!grades.isEmpty()){ 43 for (int i = 0; i < grades.size(); i++) { 44 Element element = grades.get(i); 45 System.out.println(element.getName()+element.attributeValue("name")); 46 @SuppressWarnings("unchecked") 47 List<Element> classes = element.elements(); 48 if(classes.size()>0){ 49 for (int j = 0; j < classes.size(); j++) { 50 Element cls = classes.get(j); 51 @SuppressWarnings("unchecked") 52 List<Element> stus = cls.elements(); 53 if(!stus.isEmpty()){ 54 for (int k = 0; k < stus.size(); k++) { 55 Element stu = stus.get(k); 56 System.out.println(stu.getName()+stu.attributeValue("name")); 57 58 //以下证明elements方法返回值不会为null,另查看源码发现也是直接创建list 59 // List list = stu.elements(); 60 // System.out.println(list==null); 61 // System.out.println(list.size()); 62 // System.out.println(list); 63 } 64 } 65 66 } 67 } 68 } 69 } 70 } 71 72 /** 73 * @Title: getRandoStr 74 * @Description:获取一个随机字符串 75 * @param @param len 76 * @param @return 参数 77 * @return String 返回类型 78 */ 79 public static String getRandoStr(int len){ 80 String base = "abcdefghijklmnopqrstuvwxyz1234567890"; 81 Random random = new Random(); 82 StringBuilder sb = new StringBuilder(); 83 for (int i = 0; i < len; i++) { 84 sb.append(base.charAt(random.nextInt(base.length()))); 85 } 86 87 return sb.toString(); 88 } 89 90 /** 91 * @Title: write 92 * @Description: 测试写一个school文件 93 * @param @throws IOException 参数 94 * @return void 返回类型 95 */ 96 public static void write(String path) throws IOException{ 97 //创建doc文档 98 Document doc = DocumentHelper.createDocument(); 99 Element root = DocumentHelper.createElement("school"); 100 //设置根元素 101 doc.setRootElement(root); 102 //添加属性 103 root.addAttribute("name","实验中学"); 104 root.addAttribute("position","河北石家庄"); 105 106 Element grade = root.addElement("grade"); 107 grade.addAttribute("name", "年级"); 108 grade.addAttribute("size", "1"); 109 Element cls = grade.addElement("class"); 110 cls.addAttribute("name", "班级"); 111 cls.addAttribute("size", "70"); 112 Element stu = cls.addElement("student"); 113 stu.addAttribute("name", getRandoStr(4)); 114 stu.addAttribute("age", 13+""); 115 stu.addAttribute("sex", new Random().nextInt(2)+""); 116 stu.setText("学生"); 117 118 // //添加三个子节点初一初二初三 119 // for (int i = 1; i < 4; i++) { 120 // Element grade = root.addElement("grade"); 121 // grade.addAttribute("name", "grade_"+i); 122 // grade.addAttribute("size", "20"); 123 // //添加20个班级 124 // for (int j = 1; j < 21; j++) { 125 // Element cls = grade.addElement("class"); 126 // cls.addAttribute("name", "grade_"+i+"_class_"+j); 127 // cls.addAttribute("size", "70"); 128 // //添加70个学生 129 // for (int k = 1; k < 71; k++) { 130 // Element stu = cls.addElement("student"); 131 // stu.addAttribute("name", getRandoStr(4)); 132 // stu.addAttribute("age", 12+i+""); 133 // stu.addAttribute("sex", new Random().nextInt(2)+""); 134 // stu.setText("学生"); 135 // } 136 // } 137 // } 138 139 140 //输出 141 //控制台 142 new XMLWriter().write(doc); 143 144 //文件 145 //格式 -缩进\t true表示换行 146 OutputFormat format = new OutputFormat("\t",true); 147 XMLWriter writer = new XMLWriter(new FileOutputStream(path),format); 148 writer.write(doc); 149 writer.flush(); 150 writer.close(); 151 152 } 153 154 //解析xml字符串 155 public static Document parseStr(String xmlStr) throws DocumentException{ 156 Document doc = DocumentHelper.parseText(xmlStr); 157 return doc; 158 } 159 160 public static void main(String[] args) throws IOException, DocumentException { 161 // write("src/main/java/com/test/xml/school.xml"); 162 163 read("src/main/java/com/test/xml/school.xml"); 164 165 } 166 167 }
本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/4956209.html,如需转载请自行联系原作者