一、依赖包
dom4j-1.6.1.jar
xml-apis-1.0.b2.jar
二、读XML
1.XML文件:emplist.xml
<?xml version="1.0" encoding="UTF-8"?> <list> <emp id="1"> <name>张山1</name> <age>23</age> <gender>男</gender> <salary>8000</salary> </emp> <emp id="2"> <name>张山2</name> <age>25</age> <gender>男</gender> <salary>7000</salary> </emp> <emp id="3"> <name>张山3</name> <age>25</age> <gender>男</gender> <salary>6000</salary> </emp> <emp id="4"> <name>张山4</name> <age>36</age> <gender>男</gender> <salary>5000</salary> </emp> <emp id="5"> <name>张山5</name> <age>21</age> <gender>男</gender> <salary>4000</salary> </emp> </list>
2.解析XML文件代码:ParseXmlDemo.java
package XML; import java.io.File; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /* * 解析XML */ public class ParseXmlDemo { public static void main(String[] args) { try { /* * 使用DOM4j解析xml大致步骤: * 1.创建SAXReader * 2.使用SAXReader读取要解析的文档,并生成Document对象 * 3.通过Document对象获取根元素 * 4.按照XML的树结构从根元素开始逐级获取子元素以及对应的数据达到 * 变量XML文档数据的目的 */ SAXReader reader=new SAXReader(); Document doc=reader.read(new File("emplist.xml")); /* * 获取根元素 * Element的每一个实例用于表示XML文档中的一个元素(一对标签) * 常用方法: * String getName():获取元素名 * String getText():获取元素中间的文本(开始与结束之间的文本) * Element element(String name):获取当前元素中指定标签名字的子元素 * List elements():获取当前元素中所有子元素 * List elements(String name):获取当前元素中所有同名子元素(指定的名字) * */ Element root=doc.getRootElement(); System.out.println(root.getName()); /* * 将根标签中每个<emp>标签记录的员工信息读取出来,并以Emp实例形式保存 */ List<Emp> empList=new ArrayList<Emp>(); //获取根标签<list>所有名为<emp>的子标签 List<Element> list=root.elements("emp"); //遍历集合list中的每个<emp>标签 for(Element empEle:list) { int id=Integer.valueOf(empEle.attributeValue("id")); //获取该员工的名字 //1首先通过<emp>获取<name>子标签 Element nameEle=empEle.element("name"); //2获取<name>标签中的文本信息 String name=nameEle.getTextTrim(); int age=Integer.valueOf(empEle.element("age").getTextTrim()); String gender=empEle.element("gender").getTextTrim(); int salary=Integer.valueOf(empEle.element("salary").getTextTrim()); System.out.println("id:"+id+","+"name:"+name+","+"age:"+age+","+"gender:"+gender+","+"salary:"+salary); Emp emp=new Emp(id, name, age, gender, salary); empList.add(emp); } System.out.println(empList); for(Emp e:empList) { System.out.println(e); } } catch (Exception e) { e.printStackTrace(); } } }
三、写XML文件
1.员工类(Emp.java),用于生成向XML文件写入的信息
package XML; /* * 员工类,该类的每一个实例用于表示一个员工 */ public class Emp { private int id; private String name; private int age; private String gender; private int salary; public Emp(int id, String name, int age, String gender, int salary) { super(); this.id = id; this.name = name; this.age = age; this.gender = gender; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public String toString() { //return "("+id+","+name+","+age+","+gender+","+salary+")"; return id+","+name+","+age+","+gender+","+salary; } }
2.生成XML文件代码(writerXmlDemo.java)
package XML; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /* * 写出XML文档 */ public class writerXmlDemo { public static void main(String[] args) { /* * 生成一个XML文档的步骤: * 1.创建一个Document对象 * 2.想Document中添加根元素 * 3.向根元素中逐级添加子元素以形成XML文档的数结构 * 4.创建XmlWriter * 5.将Document对象用XmlWriter写出,以形成XML文档 * 6.关闭XmlWriter */ List<Emp> empList=new ArrayList<Emp>(); empList.add(new Emp(1, "张山1", 23, "男", 8000)); empList.add(new Emp(2, "张山2", 25, "男", 7000)); empList.add(new Emp(3, "张山3", 25, "男", 6000)); empList.add(new Emp(4, "张山4", 36, "男", 5000)); empList.add(new Emp(5, "张山5", 21, "男", 4000)); XMLWriter writer=null; try { Document doc= DocumentHelper.createDocument(); //添加根元素,此方法只能执行一次,并且以Element的形式将其返回 Element root=doc.addElement("list"); //遍历empList集合,将每个Emp实例以一个<emp>标签的形式添加到根标签中 for(Emp e:empList ) { //向根标签中添加一个<emp>标签 Element empEle=root.addElement("emp"); empEle.addAttribute("id", e.getId()+"");//添加属性 Element nameEle=empEle.addElement("name");//添加<name>标签 nameEle.addText(e.getName());//给<name>标签添加文本 empEle.addElement("age").addText(e.getAge()+""); empEle.addElement("gender").addText(e.getGender()); empEle.addElement("salary").addText(e.getSalary()+""); } //OutputFormat.createPrettyPrint()表示創建的XML文件格式不在一行 writer=new XMLWriter(new FileOutputStream("myemp.xml"),OutputFormat.createPrettyPrint()); writer.write(doc);//将Document对象按照其结构写出XML文件 System.out.println("写出完毕!"); } catch (Exception e) { e.printStackTrace(); }finally { if(writer!=null) { try { writer.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } }
3.执行上面的代码生成如下XML文件(myemp.xml)
<?xml version="1.0" encoding="UTF-8"?> <list> <emp id="1"> <name>张山1</name> <age>23</age> <gender>男</gender> <salary>8000</salary> </emp> <emp id="2"> <name>张山2</name> <age>25</age> <gender>男</gender> <salary>7000</salary> </emp> <emp id="3"> <name>张山3</name> <age>25</age> <gender>男</gender> <salary>6000</salary> </emp> <emp id="4"> <name>张山4</name> <age>36</age> <gender>男</gender> <salary>5000</salary> </emp> <emp id="5"> <name>张山5</name> <age>21</age> <gender>男</gender> <salary>4000</salary> </emp> </list>