//Book类
package com.imooc.entity;
public class Book {
private String id;
private String name;
private String author;
private String year;
private String price;
private String language;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
//DOM4Jtest类
package com.imooc.dom4jtest;
import com.imooc.entity.Book;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class DOM4JTest {
private static ArrayList<Book> booksList = new ArrayList<Book>();
private void parseXML() {
//解析books.xml文件
//创建SAXReader的对象reader
SAXReader reader = new SAXReader();
try {
//通过reader对象的read方法加载books.xml文件,获取document对象
Document document = (Document) reader.read(new File("src/resource/books.xml"));
//通过document对象获取根节点bookStore
Element bookStore = (Element) document.getRootElement();
//element对象的elementIterator方法获取迭代器
Iterator it = bookStore.elementIterator();
//遍历迭代器,获取根节点中的信息(书籍)
while (it.hasNext()) {
Book bookentity = new Book();
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());
if (attr.getName().equals("id")) {
bookentity.setId(attr.getName() + "=" + attr.getValue());
}
}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
//获取book标签的子节点
Element bookChild = (Element) itt.next();
System.out.println("节点名: " + bookChild.getName() + "--节点值: " + bookChild.getStringValue());
if (bookChild.getName().equals("name")) {
bookentity.setName(bookChild.getStringValue());
} else if (bookChild.getName().equals("author")) {
bookentity.setAuthor(bookChild.getStringValue());
} else if (bookChild.getName().equals("year")) {
bookentity.setYear(bookChild.getStringValue());
} else if (bookChild.getName().equals("price")) {
bookentity.setPrice(bookChild.getStringValue());
} else if (bookChild.getName().equals("language")) {
bookentity.setLanguage(bookChild.getStringValue());
}
}
booksList.add(bookentity);
bookentity = null;
System.out.println("==============结束遍历某一本书================");
System.out.println("现在有: " + booksList.size() + "本书");
for (Book books : booksList) {
System.out.println(books.getId());
System.out.println(books.getName());
System.out.println(books.getAuthor());
System.out.println(books.getYear());
System.out.println(books.getPrice());
System.out.println(books.getLanguage());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
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");
title.setText("国内最新新闻");
Element image = channel.addElement("image");
Element title1 = image.addElement("title");
title1.setText("news.baidu.com");
Element link = image.addElement("link");
link.setText("http://news.baidu.com");
Element url = image.addElement("url");
url.setText("http://news.baidu.com/img/logo-news.gif");
Element description = channel.addElement("description");
description.setText("百度新闻订阅");
Element link1 = channel.addElement("link");
link1.setText("http://news.baidu.com");
Element language = channel.addElement("language");
language.setText("zh-cn");
Element docs = channel.addElement("docs");
docs.setText("http://blogs.law.harvard.edu/tech/rss");
Element generator = channel.addElement("generator");
generator.setText("www.baidu.com");
Element ttl = channel.addElement("ttl");
ttl.setText("5");
Element item = channel.addElement("item");
Element title2 = item.addElement("title");
title2.setText("<![CDATA[上海移动互联网产业促进中心正式揭牌]]>");
Element link2 = item.addElement("link");
link2.setText("----------------结束-----------------");
//5.设置生成xml的格式,使节点换行并缩进
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
//6.生成xml文件
File file = new File("rssnews.xml");
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
//设置是否对"<>"进行转义,默认值是true,代表转义
writer.setEscapeText(false);
writer.write(document);
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new DOM4JTest().createXML();
}
}