首先去下载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;
}
}
下面是测试类
public class TestCreateXML {
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 version="1.0" encoding="utf-8"?>
<teacher/>
这个是只有一个根节点的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");
}
执行看到后台打印出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>
这就完成生成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());
}
}
执行这个方法,后台打印出
student1
ding
040931
可以看到读取到节点student1以及这个节点下的内容
将Element student = rootElement.element("student1");这里的student1改成student2
执行看到
student2
laoding
040931
好了,完成了,有什么疑问,欢迎交流
posted on 2008-09-08 20:16
老丁 阅读(1091)
评论(0) 编辑 收藏 所属分类:
xml