本文章使用简单的xml作为事例,展现了使用Dom4j来对xml文件进行操作的一系列代码以及相关问题,在使用Dom4j之前,我们需要下载Dom4j的压缩包,并使用到项目中去,这里不做说明,其中xml代码较为简单,如下:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<p1 id1 ="aaaa">
<name>zhangsan</name>
<age>4000</age>
<sex>nv</sex>
</p1>
<p1>
<name>lisi</name>
<age>20</age>
</p1>
</person>
对于xml的相关操作代码如下:
package cn.itcast.dom4j;
import java.io.FileOutputStream;
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 cn.itcast.utils.Dom4jUtils;
public class TextDom4j {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//selectNameValue();
//selectFristNameValue();
//selectSecondNameValue();
//addNode();
//addAgeBefore();
//removeNode();
//deleteNode();
getNodeValue();
}
private static void getNodeValue() {
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
Element root = document.getRootElement();
Element p1 = root.element("p1");
String value = p1.attributeValue("id1");
System.out.println(value);
}
private static void deleteNode() {
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
Element root = document.getRootElement();
Element p1 = root.element("p1");
Element school = p1.element("school");
//school.getParent();
p1.remove(school);
Dom4jUtils.rollBack(Dom4jUtils.PATH, document);
}
private static void removeNode() throws Exception {
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
Element root = document.getRootElement();
Element p1 = root.element("p1");
Element age = p1.element("age");
age.setText("4000");
Dom4jUtils.rollBack(Dom4jUtils.PATH, document);
}
private static void addAgeBefore() throws Exception {
// SAXReader saxReader = new SAXReader();
//
// Document document = saxReader.read("src/p.xml");
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
Element root = document.getRootElement();
Element p1 = root.element("p1");
List<Element> list = p1.elements();
Element school =DocumentHelper.createElement("school");
school.setText("xiaoxue");
list.add(1, school);
Dom4jUtils.rollBack(Dom4jUtils.PATH, document);
// OutputFormat format = OutputFormat.createPrettyPrint();
//
// XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/p.xml"), format);
//
// xmlWriter.write(document);
//
// xmlWriter.close();
}
private static void addNode() throws Exception {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/p.xml");
Element root = document.getRootElement();
Element p1 = root.element("p1");
Element sex = p1.addElement("sex");
sex.setText("nv");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter( new FileOutputStream("src/p.xml"),format);
xmlWriter.write(document);
xmlWriter.close();
}
private static void selectSecondNameValue() throws Exception {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/p.xml");
Element root =document.getRootElement();
List<Element> list = root.elements("p1");
Element p2 = list.get(1);
Element name2 = p2.element("name");
String s2 = name2.getText();
System.out.println(s2);
}
private static void selectFristNameValue() throws Exception {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/p.xml");
Element root = document.getRootElement();
Element p1 = root.element("p1");
Element name = p1.element("name");
String nameValue = name.getText();
System.out.println(nameValue);
}
private static void selectNameValue() throws Exception {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/p.xml");
Element root = document.getRootElement();
List<Element> list = root.elements("p1");
for (Element element : list) {
Element name = element.element("name");
String nameValue = name.getText();
System.out.println(nameValue);
}
}
}
由于在书写的过程中,存在大量重复的代码,在后期的书写中提取了代码作为另外一个java文件,如下:
package cn.itcast.utils;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jUtils {
public static final String PATH="src/p.xml";
public static Document getDocument(String path)
{
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(path);
return document;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void rollBack(String path,Document document) {
try{
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format);
xmlWriter.write(document);
xmlWriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
}