1、环境准备
要想使用Dom4j来解析xml文件,就先得有Dom4j对应的文件。
下载路径:
1.官网下载: http://www.dom4j.org/dom4j-1.6.1/
2.dom4j是sourceforge.net上的一个开源项目,因此可以到http://sourceforge.net/projects/dom4j下载其最新版
将下载完成的zip文件进行解压,然后将解压文件中的dom4d-1.6.1jar文件放到项目中,然后右键该文件Build Path -> Add to Build Path,就可以使用了。
具体的步骤查看:http://blog.youkuaiyun.com/redarmy_chen/article/details/12969219 博客。
二、dom4j解析
1,在解析xml文件之前要先认识xml文件,对吧。
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这一行表示了版本信息和编码格式 -->
<dogs> <!-- <dogs></dogs>这个标签代表了根节点 -->
<dog id="1"> <!-- 代表节点以及属性 -->
<name>二哈</name>
<color>黑白相间</color>
</dog>
<dog id="2">
<name>金毛</name>
<color>黄色</color>
</dog>
</dogs>
2,解析XML过程是通过获取Document对象,然后继续获取各个节点以及属性等操作,因此获取Document对象是第一步,大体说来,有三种方式。
//1, 自己创建Document对象
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
//2, 读取已有的文件获得Document对象
SAXReader read = new SAXReader();
Document doc = read.read("xxx.xml");
//3, 解析xml形式的文本获得Document对象
String text = "<root></root>";
Document doc = DocumentHelper.parseText(text);
3,操作节点对象
//1, 获取文档的根节点
Element root = doc.getRootElement();
//2, 获取节点的某个节点
Element ele = root.element("xxx");
//3, 获得节点的文本
String text = ele.getText();
//4, 获得节点下的所有节点
List<Element> lists = root.elements();
//5, 获得节点下的名为xxx的所有节点
List<Element> lists = root.elements("xxx");
//6, 遍历节点
for(Element list : lists){
//TODO something
}
//7, 在节点下添加名为xxx的子节点
Element ele = root.addElement("xxx");
//8, 设置节点的文本
ele.setText("xxxx");
//9, 删除节点 childElement是被删除的节点,parentElement为其父节点
parentElement.remove(childElement);
4,操作节点属性
Element element = root.element("xxx");
//1, 获得节点的某个属性
Attribute attribute = element.attribute("id");
//2, 获得节点的所有属性
List<Attribute> lists = element.attributes();
//3, 获得节点属性的文字
String text = attribute.getText();
//4, 遍历节点属性
for(Attribute att : lists){
// to do something
}
//5, 增加节点属性名和属性值
element.addAttribute("id", "1");
//6, 设置属性值
attribute.setText("xxxx");
//7, 删除属性
element.remove(attribute);
5,写入xml文件
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Test1 {
public static void main(String[] args) {
// 自己创建document对象
Document doc = DocumentHelper.createDocument();
// 添加根节点
Element root = doc.addElement("animal");
// 创建节点
Element dog1 = root.addElement("dog");
dog1.addAttribute("id", "1"); //添加属性
// 在节点下添加子节点
Element name1 = dog1.addElement("name");
name1.setText("二哈");
Element color1 = dog1.addElement("color");
color1.setText("黑白相间");
//创建第二个狗类节点
Element dog2 = root.addElement("dog");
dog2.addAttribute("id", "2");
// 在节点下添加子节点
Element name2 = dog2.addElement("name");
name2.setText("金毛") ;
Element color2 = dog2.addElement("color");
color2.setText("黑白相间");
//创建猫类类节点
Element cat1 = root.addElement("cat");
cat1.addAttribute("id", "3");
// 在节点下添加子节点
Element cat1Name = cat1.addElement("name");
cat1Name.setText("橘猫");
Element cat1Color = cat1.addElement("color");
cat1Color.setText("黄白条纹");
//将document对象写入xml文件中
//设置xml的排版格式
// OutputFormat format = OutputFormat.createCompactFormat(); //这个格式排版紧密,阅读性较差
OutputFormat format = OutputFormat.createPrettyPrint(); //这个格式排版宽松,阅读性较好
format.setEncoding("UTF-8"); //设置编码格式
//创建XMLWriter对象,进行写入
XMLWriter write = null;
try {
//通俗点说:将document对象使用format格式写到animal.xml文件中
write = new XMLWriter(new FileOutputStream("animal.xml"), format);
write.write(doc); //写入document对象
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(write != null){
try {
//关闭操作
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
使用宽松排版写出来的文件
<?xml version="1.0" encoding="UTF-8"?>
<animal>
<dog id="1">
<name>二哈</name>
<color>黑白相间</color>
</dog>
<dog id="2">
<name>金毛</name>
<color>黄色</color>
</dog>
<cat id="3">
<name>橘猫</name>
<color>黄白条纹</color>
</cat>
</animal>
使用紧凑排版写出来的文件
<?xml version="1.0" encoding="UTF-8"?>
<animal><dog id="1"><name>二哈</name><color>黑白相间</color></dog><dog id="2"><name>金毛</name><color>黄色</color></dog><cat id="3"><name>橘猫</name><color>黄白条纹</color></cat></animal>
6,解析xml文件
package XML解析;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test2 {
public static void main(String[] args) {
//创建SAXReader对象读取xml文件
SAXReader read = new SAXReader();
try {
//读取xml文件返回document对象
Document doc = read.read("animal.xml");
//获得根节点
Element root = doc.getRootElement();
//获取root节点下所有狗类的节点
List<Element> lists = root.elements("dog");
//遍历所有的狗类节点
for(Element list : lists){
//获得dog类节点的属性
List<Attribute> attrs = list.attributes();
// 遍历属性
for(Attribute attr : attrs){
System.out.println(attr.getName()+" : "+attr.getText());
}
//遍历dog类节点下面的子节点
List<Element> lis = list.elements();
for(Element li : lis){
System.out.println(li.getName()+": "+li.getText());
}
System.out.println("----------------------------");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
遍历结果
id : 1
name: 二哈
color: 黑白相间
----------------------------
id : 2
name: 金毛
color: 黄色
----------------------------
这里只写了新手入门的东西,其他的还可以去http://blog.youkuaiyun.com/redarmy_chen/article/details/12969219进行查看。