文章目录
persons.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="a">
<name>zhangsan</name>
<sex>男</sex>
</person>
<person>
<name>lisi</name>
<age>42</age>
</person>
</persons>
案例:
1.查询xml中所有name元素的值:
public static void selectName() throws DocumentException {
//创建解析器
SAXReader saxReader = new SAXReader();
//得到document
Document document = saxReader.read("./src/com/example/dom4j/persons.xml");
//获取根节点
Element rootElement = document.getRootElement();
//得到person
List<Element> persons = rootElement.elements("person");
for (Element person : persons) {
Element name = person.element("name");
System.out.println(name.getText());
}
}
2.获取第一个name元素的值:
public static void selectSingle() throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("./src/com/example/dom4j/persons.xml");
Element root = document.getRootElement();
Element person = root.element("person");
Element name = person.element("name");
System.out.println(name.getText());
}
3.获取第二个name元素的值:
public static void selectSecond() throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("./src/com/example/dom4j/persons.xml");
Element root = document.getRootElement();
List<Element> elements = root.elements("person");
Element person = elements.get(1);
Element name = person.element("name");
System.out.println(name.getText());
}
4.在第一个person标签末尾添加一个元素男:
public static void addSex() throws DocumentException, IOException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("./src/com/example/dom4j/persons.xml");
Element root = document.getRootElement();
//获取到第一个person
Element person = root.element("person");
Element sex = person.addElement("sex");
sex.addText("男");
//回写
XMLWriter writer = new XMLWriter(new FileOutputStream("./src/com/example/dom4j/persons.xml"),
OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
}
5.在第一个person标签的age标签之前添加元素:
public static void addAgeBefore() throws DocumentException, IOException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("./src/com/example/dom4j/persons.xml");
Element root = document.getRootElement();
Element person = root.element("person");
//获取到person下的所有元素
List<Element> elements = person.elements();
//创建元素
Element school = DocumentHelper.createElement("school");
school.setText("jgs");
//在特定位置添加
elements.add(1, school);
//回写
XMLWriter writer = new XMLWriter(new FileOutputStream("./src/com/example/dom4j/persons.xml"),
OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
}
工具类:
public class Dom4jUtils {
//返回document
public static Document getDocument(String path) {
try {
//创建解析器
SAXReader saxReader = new SAXReader();
//获取document
return saxReader.read(path);
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
//以PrettyPrint格式回写xml方法
public static void writerPretty(String path, Document document) {
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(path), OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.修改第一个person下面的age元素标签:
public static void modifyAge() {
String path = "./src/com/example/dom4j/persons.xml";
//得到document
Document document = Dom4jUtils.getDocument(path);
if (document != null) {
Element root = document.getRootElement();
//获取第一个person
Element person = root.element("person");
//获取age标签
Element age = person.element("age");
age.setText("25");
Dom4jUtils.writerPretty(path, document);
}
}
6.删除第一个person下面的school标签:
public static void delSchool() {
String path = "./src/com/example/dom4j/persons.xml";
Document document = Dom4jUtils.getDocument(path);
if (document != null) {
Element root = document.getRootElement();
Element person = root.element("person");
Element school = person.element("school");
person.remove(school);
Dom4jUtils.writerPretty(path, document);
}
}
7.获取第一个person里的属性id的值:
public static void getValues() {
String path = "./src/com/example/dom4j/persons.xml";
Document document = Dom4jUtils.getDocument(path);
if (document != null) {
Element root = document.getRootElement();
Element person = root.element("person");
String id = person.attributeValue("id");
System.out.println(id);
}
}