首先去下載dom4j的jar包,新建WEB工程,將jar包加到工程當中
準備一個操作類 FindXmlDoc.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class FindXmlDoc {
/**
*
* @Description 創建xml文件
* @param filename
* @param rootElement
* void
* @throws 拋出異常說明
*/
public void createXMLFile(String filename,String rootElement) {
/** 建立document對象 */
Document document = DocumentHelper.createDocument();
document.addElement(rootElement);
try {
/** 將document中的內容寫入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
} catch (Exception ex) {
}
}
/**
*
* @Description 保存編輯的XML文件
* @param document
* @param filepath
* @return
* boolean
* @throws 拋出異常說明
*/
public boolean saveDocument(Document document,String filepath) {
boolean isOk = false;
Writer writer = null;
XMLWriter xmlWriter = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(filepath),"utf-8");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
xmlWriter = new XMLWriter(writer,format);
xmlWriter.write(document);
writer.close();
isOk = true;
} catch (IOException e) {
return false;
}
return isOk;
}
/**
*
* @Description 獲取XML文件中的信息,如果文件存在,返回document,如果不存在,返回null
* @param filepath
* @return
* Document
* @throws 拋出異常說明
*/
public Document getDocument(String filepath) {
Document document = null;
SAXReader reader = new SAXReader();
try {
File file = new File(filepath);
if(file.exists()){
document = reader.read(file);//不是以UTF-8來讀取。要換成以UTF——8來讀取
}else{
return null;
}
} catch (Exception e) {
}
return document;
}
}
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class FindXmlDoc {
/**
*
* @Description 創建xml文件
* @param filename
* @param rootElement
* void
* @throws 拋出異常說明
*/
public void createXMLFile(String filename,String rootElement) {
/** 建立document對象 */
Document document = DocumentHelper.createDocument();
document.addElement(rootElement);
try {
/** 將document中的內容寫入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
} catch (Exception ex) {
}
}
/**
*
* @Description 保存編輯的XML文件
* @param document
* @param filepath
* @return
* boolean
* @throws 拋出異常說明
*/
public boolean saveDocument(Document document,String filepath) {
boolean isOk = false;
Writer writer = null;
XMLWriter xmlWriter = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(filepath),"utf-8");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
xmlWriter = new XMLWriter(writer,format);
xmlWriter.write(document);
writer.close();
isOk = true;
} catch (IOException e) {
return false;
}
return isOk;
}
/**
*
* @Description 獲取XML文件中的信息,如果文件存在,返回document,如果不存在,返回null
* @param filepath
* @return
* Document
* @throws 拋出異常說明
*/
public Document getDocument(String filepath) {
Document document = null;
SAXReader reader = new SAXReader();
try {
File file = new File(filepath);
if(file.exists()){
document = reader.read(file);//不是以UTF-8來讀取。要換成以UTF——8來讀取
}else{
return null;
}
} catch (Exception e) {
}
return document;
}
}
下面是測試類
public class TestCreateXML {
public static void main(String[] args) {
TestCreateXML.createXML();
}
public static void createXML(){
FindXmlDoc fxd = new FindXmlDoc();
fxd.createXMLFile("teacher.xml", "teacher");
}
}
public static void main(String[] args) {
TestCreateXML.createXML();
}
public static void createXML(){
FindXmlDoc fxd = new FindXmlDoc();
fxd.createXMLFile("teacher.xml", "teacher");
}
}
執行這個類就會在webroot下生成teacher.xml文件
內容如下:





這個是只有一個根節點的xml文件。
下面來加點東西
修改createXML方法為如下:
public static void createXML(){
FindXmlDoc fxd = new FindXmlDoc();
String path ="teacher.xml";
Document document = fxd.getDocument(path);
Element rootElement = document.getRootElement();
Element student1Element = rootElement.addElement("student1");
Element student1name = student1Element.addElement("name");
Element student1class = student1Element.addElement("class");
student1name.addText("ding");
student1class.addText("040931");
Element student2Element = rootElement.addElement("student2");
Element student2name = student2Element.addElement("name");
Element student2class = student2Element.addElement("class");
student2name.addText("laoding");
student2class.addText("040931");
fxd.saveDocument(document, path);
System.out.println("success");
}
FindXmlDoc fxd = new FindXmlDoc();
String path ="teacher.xml";
Document document = fxd.getDocument(path);
Element rootElement = document.getRootElement();
Element student1Element = rootElement.addElement("student1");
Element student1name = student1Element.addElement("name");
Element student1class = student1Element.addElement("class");
student1name.addText("ding");
student1class.addText("040931");
Element student2Element = rootElement.addElement("student2");
Element student2name = student2Element.addElement("name");
Element student2class = student2Element.addElement("class");
student2name.addText("laoding");
student2class.addText("040931");
fxd.saveDocument(document, path);
System.out.println("success");
}
執行看到后臺打印出success
此時發現teacher.xml變成了以下樣子
<?xml version="1.0" encoding="utf-8"?>
<teacher>
<student1>
<name>ding</name>
<class>040931</class>
</student1>
<student2>
<name>laoding</name>
<class>040931</class>
</student2>
</teacher>
<teacher>
<student1>
<name>ding</name>
<class>040931</class>
</student1>
<student2>
<name>laoding</name>
<class>040931</class>
</student2>
</teacher>
這就完成生成xml的操作了
下面是讀取xml文件
public static void readXML(){
FindXmlDoc fxd = new FindXmlDoc();
String path ="teacher.xml";
Document document = fxd.getDocument(path);
Element rootElement = document.getRootElement();
Element student = rootElement.element("student1");
System.out.println(student.getName());
List l = student.elements();
for(int i = 0; i < l.size(); i++){
Element e= (Element)l.get(i);
System.out.println(e.getText());
}
}
FindXmlDoc fxd = new FindXmlDoc();
String path ="teacher.xml";
Document document = fxd.getDocument(path);
Element rootElement = document.getRootElement();
Element student = rootElement.element("student1");
System.out.println(student.getName());
List l = student.elements();
for(int i = 0; i < l.size(); i++){
Element e= (Element)l.get(i);
System.out.println(e.getText());
}
}
執行這個方法,后臺打印出
student1
ding
040931
可以看到讀取到節點student1以及這個節點下的內容
將Element student = rootElement.element("student1");這里的student1改成student2
執行看到
student2
laoding
040931
好了,完成了,有什么疑問,歡迎交流