在java中使用dom4j解析xml(示例代码)
虽然java中已经有了dom和sax这两种标准解析方式
但其操作起来并不轻松,对于我这么一个初学者来说,其中部分代码是活生生的恶心
为此,伟大的第三方开发组开发出了jdom和dom4j等工具
鉴于目前的趋势,我们这里来讲讲dom4j的基本用法,不涉及递归等复杂操作
dom4j的用法很多,官网上的示例有那么点儿晦涩,这里就不写了
首先我们需要出创建一个xml文档,然后才能对其解析
xml文档:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="001">
<title>harry potter</title>
<author>j k. rowling</author>
</book>
<book id="002">
<title>learning xml</title>
<author>erik t. ray</author>
</book>
</books>
示例一:用list列表的方式来解析xml
import java.io.file;
import java.util.list;
import org.dom4j.attribute;
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
public class demo {
public static void main(string[] args) throws exception {
saxreader reader = new saxreader();
file file = new file("books.xml");
document document = reader.read(file);
element root = document.getrootelement();
list<element> childelements = root.elements();
for (element child : childelements) {
//未知属性名情况下
/*list<attribute> attributelist = child.attributes();
for (attribute attr : attributelist) {
system.out.println(attr.getname() + ": " + attr.getvalue());
}*/
//已知属性名情况下
system.out.println("id: " + child.attributevalue("id"));
//未知子元素名情况下
/*list<element> elementlist = child.elements();
for (element ele : elementlist) {
system.out.println(ele.getname() + ": " + ele.gettext());
}
system.out.println();*/
//已知子元素名的情况下
system.out.println("title" + child.elementtext("title"));
system.out.println("author" + child.elementtext("author"));
//这行是为了格式化美观而存在
system.out.println();
}
}
}
示例二:使用iterator迭代器的方式来解析xml
import java.io.file;
import java.util.iterator;
import org.dom4j.attribute;
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
public class demo {
public static void main(string[] args) throws exception {
saxreader reader = new saxreader();
document document = reader.read(new file("books.xml"));
element root = document.getrootelement();
iterator it = root.elementiterator();
while (it.hasnext()) {
element element = (element) it.next();
//未知属性名称情况下
/*iterator attrit = element.attributeiterator();
while (attrit.hasnext()) {
attribute a = (attribute) attrit.next();
system.out.println(a.getvalue());
}*/
//已知属性名称情况下
system.out.println("id: " + element.attributevalue("id"));
//未知元素名情况下
/*iterator eleit = element.elementiterator();
while (eleit.hasnext()) {
element e = (element) eleit.next();
system.out.println(e.getname() + ": " + e.gettext());
}
system.out.println();*/
//已知元素名情况下
system.out.println("title: " + element.elementtext("title"));
system.out.println("author: " + element.elementtext("author"));
system.out.println();
}
}
}
运行结果:
示例三:创建xml文档并输出到文件
import java.io.file;
import java.io.fileoutputstream;
import org.dom4j.document;
import org.dom4j.documenthelper;
import org.dom4j.element;
import org.dom4j.io.outputformat;
import org.dom4j.io.xmlwriter;
public class demo {
public static void main(string[] args) throws exception {
document doc = documenthelper.createdocument();
//增加根节点
element books = doc.addelement("books");
//增加子元素
element book1 = books.addelement("book");
element title1 = book1.addelement("title");
element author1 = book1.addelement("author");
element book2 = books.addelement("book");
element title2 = book2.addelement("title");
element author2 = book2.addelement("author");
//为子节点添加属性
book1.addattribute("id", "001");
//为元素添加内容
title1.settext("harry potter");
author1.settext("j k. rowling");
book2.addattribute("id", "002");
title2.settext("learning xml");
author2.settext("erik t. ray");
//实例化输出格式对象
outputformat format = outputformat.createprettyprint();
//设置输出编码
format.setencoding("utf-8");
//创建需要写入的file对象
file file = new file("d:" + file.separator + "books.xml");
//生成xmlwriter对象,构造函数中的参数为需要输出的文件流和格式
xmlwriter writer = new xmlwriter(new fileoutputstream(file), format);
//开始写入,write方法中包含上面创建的document对象
writer.write(doc);
}
}
运行结果: