DOM4j:
DOM4j遍历XML:首先将 dom4j-2.2.1.jar包 add path到工程中
测试代码:
package com.imooc.dom4j; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class HrReader { //读取XML文件方法 public void readXml(){ String file = "/Users/lcs/eclipse-workspace/xml/src/hr.xml"; //SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中 SAXReader reader = new SAXReader(); try { Document document = reader.read(file); //获取XML文档的根节点,即hr标签 Element root = document.getRootElement(); //elements方法用于获取指定的标签集合 List<Element> employees = root.elements("employee"); for(Element employee:employees) { //element()用于获取唯一的子节点对象 Element name = employee.element("name"); //getText()标签用于获取标签文本 String empName = name.getText(); System.out.print(empName); //获取属性 Attribute att = employee.attribute("no"); System.out.println(" 工号:"+att.getText()); System.out.println(employee.elementText("age")); System.out.println(employee.elementText("salary")); Element department = employee.element("department"); System.out.println(department.element("dname").getText()); System.out.println(department.element("address").getText()); System.out.println("================"); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { HrReader reader = new HrReader(); reader.readXml(); } }
输出结果:
张三 工号:3309 30 4000 会计部 XX-大厦-B103 ================ 莉丝 工号:3310 30 5000 工厂部 XX-大厦-B104 ================
DOM4j更新XML:写入一个人员信息到hr.xml中
package com.imooc.dom4j; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class HrWriter { public void writeXml() { String file = "/Users/lcs/eclipse-workspace/xml/src/hr.xml"; // SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中 SAXReader reader = new SAXReader(); try { Document document = reader.read(file); // 获取XML文档的根节点,即hr标签 Element root = document.getRootElement(); // addElement()方法用于创造一个传入参数类型的元素 Element employee = root.addElement("employee"); // 然后在新建的employee上增加属性 employee.addAttribute("no", "3311"); // 增加子节点 Element name = employee.addElement("name"); name.setText("王五"); employee.addElement("age").setText("37"); employee.addElement("salary").setText("9000"); Element department = employee.addElement("department"); department.addElement("dname").setText("人事部"); department.addElement("address").setText("XX-大厦-A108"); // 已经在内存中完成了编写,接下来将其导入到XML文件 Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); document.write(writer); writer.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { HrWriter hrWriter = new HrWriter(); hrWriter.writeXml(); } }
查看hr.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hr SYSTEM "hr.dtd"><!-- 人力资源管理系统 --> <hr> <employee no="3309"> <name>张三</name> <age>30</age> <salary>4000</salary> <department> <dname>会计部</dname> <address>XX-大厦-B103</address> </department> </employee> <employee no="3310"> <name>莉丝</name> <age>30</age> <salary>5000</salary> <department> <dname>工厂部</dname> <address>XX-大厦-B104</address> </department> </employee> <employee no="3311"> <name>王五</name> <age>37</age> <salary>9000</salary> <department> <dname>人事部</dname> <address>XX-大厦-A108</address> </department> </employee> </hr>
案例测试
写入代码:
package com.imooc.dom4j; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Plan2Writer { public void writeXml() { String file = "/Users/lcs/eclipse-workspace/xml/src/plan2.xml"; // SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中 SAXReader reader = new SAXReader(); try { Document document = reader.read(file); // 获取XML文档的根节点,即course标签 Element root = document.getRootElement(); // addElement()方法用于创造一个传入参数类型的元素 Element employee = root.addElement("course"); // 增加子节点 Element name = employee.addElement("course-name"); name.setText("历史"); employee.addElement("class-hour").setText("50"); employee.addElement("exam-form").setText("考试"); // 已经在内存中完成了编写,接下来将其导入到XML文件 Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); document.write(writer); writer.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Plan2Writer plan2Writer = new Plan2Writer(); plan2Writer.writeXml(); } }
打开plan.xml:格式化后结果为
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plan SYSTEM "plan.dtd"> <teaching-plan> <course> <course-name>大学英语</course-name> <class-hour>36</class-hour> <exam-form>考试</exam-form> </course> <course> <course-name>高等数学</course-name> <class-hour>70</class-hour> <exam-form>考试</exam-form> </course> <course> <course-name>计算机应用基础</course-name> <class-hour>108</class-hour> <exam-form>上机考试</exam-form> </course> <course> <course-name>历史</course-name> <class-hour>50</class-hour> <exam-form>考试</exam-form> </course> </teaching-plan>
XPath的使用
Xpath是一个结合DOM4J使用的技术,XPath能够根据字符串,快速定位符合条件的标签
用dom4j+xpath技术,快速定位xml文件中的标签,并取出内容,
导入dom4j中xpath的jar包到classpath中去,
xpath也属于dom4j技术
//第一个/表示xml文件,其它/表示分隔符; String xpath = "/电视/a/b/c/d"; Element dElement = (Element) xmlDocument.selectStringleNOde(); List<Element> list = xmlDocument.selectNodes(xpath);