src/student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student number="itcast_0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
<student number="itcast_0002">
<name>jerry</name>
<age>16</age>
<sex>female</sex>
</student>
</students>
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Demo1 {
/**
* 将xml文档 加载,获得document对象,
* 将document对象 以字符串的形式输出
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
//1 创建解析器
SAXReader reader = new SAXReader();
//2 使用解析器,加载文档
Document document = reader.read(new File("src/student.xml"));
//3 将文档对象转化成string
String xml = document.asXML();
System.out.println(xml);
}
}
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo2 {
/**
* 查 ==> 文档中所有学生信息
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
//1 创建解析器
SAXReader reader = new SAXReader();
//2 使用解析器,加载文档
Document document = reader.read(new File("src/student.xml"));
//3 获得根元素
Element students = document.getRootElement();
//4 迭代根元素下的所有student元素
Iterator<Element> it = students.elementIterator("student");
while(it.hasNext()){
Element student = it.next();
//5 获得student元素上的number属性
String number = student.attributeValue("number");
System.out.println(number);
//6 再找 student元素下的name age sex 子元素
//7 获得子元素的内容
Element nameELe = student.element("name");
String name = nameELe.getText();
String age = student.elementText("age");
String sex = student.elementText("sex");
System.out.println(name+"==>"+age+"==>"+sex);
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Demo3_2 {
/**
* 增: 增加一个学生 itcast_0003 杰克 20 male
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws DocumentException, IOException {
//1 创建解析器
SAXReader reader = new SAXReader();
//2 使用解析器,加载文档
Document document = reader.read(new File("src/student.xml"));
//3 获得根元素
Element root = document.getRootElement();
//4 为根元素添加student子元素,和属性.
Element studentEle = root.addElement("student").addAttribute("number", "itcast_0003");
//5 为student添加 name age sex 子元素和内容
studentEle.addElement("name").addText("杰克");
studentEle.addElement("age").addText("20");
studentEle.addElement("sex").addText("male");
//6 回写
//1>创建格式化器
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8"); //解码 encoding="UTF-8"
//2>创建写入器
//XMLWriter writer = new XMLWriter(new FileWriter("src/student_copy.xml"),format);
//XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/student_copy.xml"),"GBK"),format);
XMLWriter writer = new XMLWriter(new FileOutputStream("src/student_copy.xml") ,format);
//3>将文档写入文件
writer.write(document);
//4>关闭资源
writer.close();
//简单的不乱码的方法: 使用字节流写入, 不要使用字符流. 使用字节流永远不会乱码.
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Demo4_2 {
/**
* 删除: 删除学号为itcast_0001 的学生
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws DocumentException, IOException {
//1 创建解析器
SAXReader reader = new SAXReader();
//2 使用解析器,加载文档
Document document = reader.read(new File("src/student.xml"));
//3 定义xpath表达式
String xpath ="//student[@number='itcast_0001']";
//4 根据xpath找出要删除的元素
Element stuEle = (Element) document.selectSingleNode(xpath);
//5 删除
stuEle.getParent().remove(stuEle);
//6回写
//1>新建格式化器
OutputFormat format = OutputFormat.createPrettyPrint();
//2>新建写入器
XMLWriter writer = new XMLWriter(new FileOutputStream("src/student_copy.xml"),format);
//3>写入
writer.write(document);
//4>关闭资源
writer.close();
}
}