Posted on 2009-07-04 16:55
胡娟 阅读(659)
评论(0) 编辑 收藏 所属分类:
JAVA
所需要的jar包
所要解析和创建的xml文档样式
<?xml version="1.0" encoding="gb2312"?>
<students>
<student id="1">
<name>hujuan</name>
<age>24</age>
</student>
<student id="2">
<name>fankai</name>
<age>24</age>
</student>
</students>
创建xml过程
package com.hujuan.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateXml {
public void createXml(){
//使用DocumentHelper类创建一个文档实例。DocumentHelper是生成xml文档节点的dom4j API工厂类
Document document = DocumentHelper.createDocument();
//使用addElement()方法创建根元素students,addElement()用于向xml文档中增加元素
Element students = document.addElement("students");
//添加注释
students.addComment("students 节点");
//在students节点下添加子节点student
Element student = students.addElement("student");
//给student元素添加属性
student.addAttribute("id", "1");
Element name = student.addElement("name");
Element age = student.addElement("age");
//给元素设置元素的文本
name.setText("hujuan");
age.setText("24");
Element student2 = students.addElement("student");
//给student元素添加属性
student2.addAttribute("id", "2");
Element name2 = student2.addElement("name");
Element age2 = student2.addElement("age");
//给元素设置元素的文本
name2.setText("fankai");
age2.setText("24");
try {
OutputFormat format = new OutputFormat(" ",true);
format.setEncoding("GB2312");
//输出创建的xml文档
XMLWriter output = new XMLWriter(format);
FileWriter fw = new FileWriter(new File("src/bb.xml"));
output.write(document);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
//在元素中使用addProcessingInstruction()方法增加一个处理指令
//students.addProcessingInstruction(arg0, arg1)
}
}
解析xml过程
package com.hujuan.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
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 ReadXml {
public void readXml(){
//使用SAXReader解析XMl
SAXReader saxReader = new SAXReader();
File file = new File("src/student.xml");
try {
Document document = saxReader.read(file);
Element students = document.getRootElement();
List list = students.elements("student");
for(Iterator it = list.iterator();it.hasNext();){
Element student = (Element)it.next();
//获得得元素的属性值
String id = student.attributeValue("id");
System.out.println(id);
//获得子元素的属性
String name = student.elementText("name");
System.out.println(name);
String age = student.elementText("age");
System.out.println(age);
//student.removeChild(name);
}
try {
OutputFormat format = new OutputFormat(" ",true);
format.setEncoding("GB2312");
//输出创建的xml文档
XMLWriter output = new XMLWriter(format);
FileWriter fw = new FileWriter(new File("src/bb.xml"));
output.write(document);
output.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
测试用例
package junit.test;
import org.junit.Test;
import com.hujuan.xml.CreateXml;
import com.hujuan.xml.ReadXml;
public class XmlTest {
@Test
public void createXml(){
CreateXml createXml = new CreateXml();
createXml.createXml();
}
@Test
public void readXml(){
ReadXml readXml = new ReadXml();
readXml.readXml();
}
}
补充
添加文档类型说明
document.addDocType("bb",null,"file://Dtds/src/bb.dtd");
输出
<!DOCTYPE bb SYSTEM "file://Dtds/src/bb.dtd"
修改name节点的值
for(Iterator ite = studentList.iterator();ite.hasNext();){
Element EName = (Element)ite.next();
EName.setText("kay");
}
修改student属性id的值
//获得student的属性
Attribute att = student.attribute("id");
//修改student的属性值
att.setValue("3");
删除student的id属性
//删除student的属性
student.remove(att);
删除student节点
//删除student节点
students.remove(student);