package util;
import java.io.FileInputStream;
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.SAXReader;
import org.dom4j.io.XMLWriter;
import entity.Emp;
/**
* 员工XML解析器
*/
public class EmpXmlParser {
/**
* 1.写XML,将员工数据写入到emp.xml中
*/
public static void emp2xml()
throws Exception {
//模拟员工数据
Emp e = new Emp();
e.setId(1);
e.setName("张三");
e.setAge(25);
e.setSalary(6000.00);
e.setType("simple");
//创建文档对象
Document doc =
DocumentHelper.createDocument();
//创建根元素
Element root = doc.addElement("emp");
//在根元素下创建各子元素
root.addElement("id").addText(
e.getId().toString());
root.addElement("name").addText(
e.getName());
root.addElement("age").addText(
e.getAge().toString());
root.addElement("salary").addText(
e.getSalary().toString());
//创建根元素的属性
root.addAttribute("type", e.getType());
//将文档对象输出
XMLWriter writer = new XMLWriter(
new FileOutputStream("d:\\xml\\emp.xml"),
OutputFormat.createPrettyPrint());
writer.write(doc);
writer.close();
}
/**
* 2.读取XML,将数据写入Emp对象中
*/
public static void xml2emp() throws Exception {
SAXReader reader = new SAXReader();
//读取xml
Document doc = reader.read(
new FileInputStream(
"d:\\xml\\emp.xml"));
Emp e = new Emp();
//读取根元素
Element root = doc.getRootElement();
//读取根元素的下级元素
e.setId(Integer.valueOf(
root.elementText("id")));
e.setName(root.elementText("name"));
e.setAge(Integer.valueOf(
root.elementText("age")));
e.setSalary(Double.valueOf(
root.elementText("salary")));
//读取根元素的属性
e.setType(root.attributeValue("type"));
System.out.println("ID:" + e.getId());
System.out.println("Name:" + e.getName());
System.out.println("Age:" + e.getAge());
System.out.println("Salary:" + e.getSalary());
System.out.println("Type:" + e.getType());
}
/**
* 将一组Emp数据写入到一个XML文件中
*/
public static void emps2xml() throws Exception {
List<Emp> emps = new ArrayList<Emp>();
Emp e1 = new Emp();
e1.setId(1);
e1.setName("张三");
e1.setAge(25);
e1.setSalary(6000.00);
e1.setType("simple");
emps.add(e1);
Emp e2 = new Emp();
e2.setId(2);
e2.setName("李四");
e2.setAge(26);
e2.setSalary(7000.00);
e2.setType("simple");
emps.add(e2);
Emp e3 = new Emp();
e3.setId(3);
e3.setName("王五");
e3.setAge(27);
e3.setSalary(8000.00);
e3.setType("Leader");
emps.add(e3);
Document doc =
DocumentHelper.createDocument();
//创建根元素
Element root = doc.addElement("emps");
for(Emp emp : emps){
Element parent = root.addElement("emp");
parent.addElement("id").addText(
emp.getId().toString());
parent.addElement("name").addText(
emp.getName());
parent.addElement("age").addText(
emp.getAge().toString());
parent.addElement("salary").addText(
emp.getSalary().toString());
parent.addAttribute("type", emp.getType());
}
//输入到XML
XMLWriter writer = new XMLWriter(
new FileOutputStream("d:\\xml\\emps.xml")
,OutputFormat.createPrettyPrint());
writer.write(doc);
writer.close();
}
/**
* 4.从XML中读取一组Emp数据
*/
public static void xml2emps() throws Exception {
SAXReader reader = new SAXReader();
List<Emp> emps = new ArrayList<Emp>();
//读取XML文件
Document doc = reader.read(
new FileInputStream(
"d:\\xml\\emps.xml"));
//读取根元素emps
Element root = doc.getRootElement();
List<Element> elems = root.elements("emp");
for(Element elem : elems){
Emp e = new Emp();
e.setId(Integer.valueOf(
elem.elementText("id")));
e.setName(elem.elementText("name"));
e.setAge(Integer.valueOf(
elem.elementText("age")));
e.setSalary(Double.valueOf(
elem.elementText("salary")));
e.setType(elem.attributeValue("type"));
emps.add(e);
}
for(Emp e : emps){
System.out.println(
e.getId() + " " +e.getName()
);
}
}
/**
* xpath演示示例
*/
public static void xpath() throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read(
new FileInputStream(
"d:\\xml\\emps.xml"));
// List elems =
// doc.selectNodes(“/emps/emp[1]”);
// List elems =
// doc.selectNodes(“/emps/emp[@type=’Leader’]”);
List elems =
doc.selectNodes(“/emps/emp[id=’2’]”);
Element parent = elems.get(0);
System.out.println(parent.elementText(“id”));
System.out.println(parent.elementText(“name”));
}
public static void main(String[] args)
throws Exception {
// emp2xml();
// xml2emp();
// emps2xml();
// xml2emps();
xpath();
}
}