XML文件
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>安徒生童话</name>
<year>2004</year>
<price>77</price>
<language>English</language>
</book>
</bookstore>
Book实体类
public class Book {
private String id;
private String name;
private String author;
private String year;
private String price;
private String language; // get set 方法生成即可 }
DOM4J解析XMl文件
public class DOM4JTest {
private static ArrayList<Book> bookList = new ArrayList<Book>(); /**
* @param args
*/
public static void main(String[] args) {
// 解析books.xml文件
// 创建SAXReader的对象reader
SAXReader reader = new SAXReader();
try {
// 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
Document document = reader.read(new File("src/res/books.xml"));
// 通过document对象获取根节点bookstore
Element bookStore = document.getRootElement();
// 通过element对象的elementIterator方法获取迭代器
Iterator it = bookStore.elementIterator();
// 遍历迭代器,获取根节点中的信息(书籍)
while (it.hasNext()) {
System.out.println("=====开始遍历某一本书=====");
Element book = (Element) it.next();
// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println("属性名:" + attr.getName() + "--属性值:" + attr.getValue());
}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());
}
System.out.println("=====结束遍历某一本书=====");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DOM4J读取XMl
public class DOM4JTest {
private void createXML() {
// 1.创建document对象,代表整个xml文档
Document document = DocumentHelper.createDocument();
// 2.创建根节点rss
Element rss = document.addElement("rss");
// 3.向rss节点中添加version属性
rss.addAttribute("version", "2.0");
// 4.生成子节点及节点内容
Element channel = rss.addElement("channel");
Element title = channel.addElement("title");
Element content = title.addElement("content");
content.setText("本周中期北方明显偏暖 多地气温较同期偏高8℃以上");
// title.setText("<![CDATA[上海移动互联网产业促进中心正式揭牌 ]]>");
// 5.设置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// 6.生成xml文件
File file = new File("rssnews.xml");
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(file), format);
// 设置是否转义,默认值是true,代表转义
writer.setEscapeText(false);
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @param args
*/
public static void main(String[] args) {
new DOM4JTest().createXML();
}
}
读取XMl