emplist.xml
<?xml version="1.0" encoding="UTF-8"?>
<list><emp id="1"><name>张三</name><age>34</age><gender>男</gender><salary>3000</salary></emp><emp id="2"><name>李四</name><age>21</age><gender>女</gender><salary>4000</salary></emp><emp id="3"><name>王五</name><age>46</age><gender>女</gender><salary>6500</salary></emp><emp id="4"><name>赵六</name><age>28</age><gender>男</gender><salary>4400</salary></emp><emp id="5"><name>钱七</name><age>53</age><gender>男</gender><salary>12000</salary></emp>
</list>
myemp.xml
<?xml version="1.0" encoding="UTF-8"?><list><emp id="1"><name>张三</name><age>22</age><gender>男</gender><salary>5000</salary></emp><emp id="2"><name>李四</name><age>23</age><gender>女</gender><salary>6000</salary></emp><emp id="3"><name>王五</name><age>24</age><gender>男</gender><salary>7000</salary></emp><emp id="4"><name>赵六</name><age>25</age><gender>女</gender><salary>8000</salary></emp><emp id="5"><name>钱七</name><age>26</age><gender>男</gender><salary>9000</salary></emp>
</list>
用当前类的实例保存xml文档中的一个员工信息 (这个是用构造方法生成的)
package day12;
/*** 用当前类的实例保存xml文档中的一个员工信息* @author adminitartor**/
public class Emp {
private int id;private String name;private int age;private String gender;private int salary;public Emp(){}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;}
}
使用DOM4J解析XML文档
package day12;import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;/*** 使用DOM4J解析XML文档* * 将emplist.xml文档中的所有员工信息解析出来* 并存入到一个List集合保存* @author adminitartor**/
public class ParseXmlDemo {
public static void main(String[] args) {try {SAXReader reader = new SAXReader();
Document doc = reader.read(new FileInputStream("emplist.xml"));System.out.println("读取xml文档完毕!");Element root = doc.getRootElement();List<Emp> empList = new ArrayList<Emp>();List<Element> list = root.elements();for(Element empEle : list){Element nameEle = empEle.element("name");String name = nameEle.getText();
int age = Integer.parseInt(empEle.elementText("age"));String gender = empEle.elementText("gender");int salary = Integer.parseInt(empEle.elementText("salary") );Attribute attri = empEle.attribute("id");int id = Integer.parseInt(attri.getValue());Emp emp = new Emp(id, name, age, gender, salary);empList.add(emp);}System.out.println("解析完毕!");for(Emp emp : empList){System.out.println(emp);}} catch (Exception e) {e.printStackTrace();} }
}
使用DOM4J生成XML文档
package day12import java.io.FileOutputStream
import java.util.ArrayList
import java.util.Listimport org.dom4j.Document
import org.dom4j.DocumentHelper
import org.dom4j.Element
import org.dom4j.io.OutputFormat
import org.dom4j.io.XMLWriter
public class WriteXmlDemo {public static void main(String[] args) {try {List<Emp> empList = new ArrayList<Emp>()empList.add(new Emp(1,"张三",22,"男",5000))empList.add(new Emp(2,"李四",23,"女",6000))empList.add(new Emp(3,"王五",24,"男",7000))empList.add(new Emp(4,"赵六",25,"女",8000))empList.add(new Emp(5,"钱七",26,"男",9000))//1 创建一个空白文档Document doc = DocumentHelper.createDocument()Element root = doc.addElement("list")for(Emp emp : empList){Element empEle = root.addElement("emp")//向<emp>标签中添加<name>标签Element nameEle = empEle.addElement("name")//向<name>标签中添加文本信息nameEle.addText(emp.getName())empEle.addElement("age").addText(emp.getAge()+"")empEle.addElement("gender").addText(emp.getGender())empEle.addElement("salary").addText(emp.getSalary()+"")empEle.addAttribute("id", emp.getId()+"")}//4XMLWriter writer = new XMLWriter(new FileOutputStream("myemp.xml"),OutputFormat.createPrettyPrint())writer.write(doc)System.out.println("写出完毕!")writer.close()} catch (Exception e) {e.printStackTrace()}}
}
使用XPath检索XML数据
package day12;import java.io.File;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;/*** 使用XPath检索XML数据* @author adminitartor**/
public class XpathDemo {
public static void main(String[] args) {try {SAXReader reader = new SAXReader();Document doc = reader.read(new File("myemp.xml"));String path = "/list/emp[gender='女' and age>24]/salary";List<Element> list = doc.selectNodes(path);for(Element e : list){System.out.println(e.getText());}} catch (Exception e) {e.printStackTrace();}}
}