XML文件--使用DOM4J示例
Posted Penney_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML文件--使用DOM4J示例相关的知识,希望对你有一定的参考价值。
- 其他依赖字段/方法
private List<Book> bookList = new LinkedList<Book>();
public List<Book> getBookList(String file) {
parseXML(file);
return this.bookList;
}
- 获取DocumentBuilder(文件解析器)
/**
* 解析XML文件
* * @param file
*/
private void parseXML(String file) {
// 创建book临时变量
Book book = null;
// 1.创建SAXReader对象
SAXReader reader = new SAXReader();
// 2.1 创建输入流
InputStreamReader input = null;
try {
// 2.2 实例化输入流,并指定编码
input = new InputStreamReader(new FileInputStream(file), "utf-8");
// 3.加载XML文件到 document
Document document = reader.read(input);
// 4.获取根节点
Element rootElement = document.getRootElement();
// 5.根据根节点获取子节点迭代器,并遍历
Iterator bookIter = rootElement.elementIterator();
while (bookIter.hasNext()) {
// 初始化book对象
book = new Book();
// 声明子节点对象
Element bookEle = (Element) bookIter.next();
// 如果子节点为book
if (bookEle.getName().equals("book")) {
// 6.获取book节点的属性迭代器,并遍历
Iterator eleIter = bookEle.attributeIterator();
while (eleIter.hasNext()) {
// 声明属性对象
Attribute attr = (Attribute) eleIter.next();
if (attr.getName().equals("id")) {
book.setId(Integer.valueOf(attr.getValue()));
}
}
// 7.获取book节点的子节点迭代器,并遍历
Iterator childIter = bookEle.elementIterator();
while (childIter.hasNext()) {
// 声明book子节点对象
Element child = (Element) childIter.next();
// 判断并为book对象赋值
switch (child.getName()) {
case "title":
book.setTitle(child.getText());
break;
case "author":
book.setAuthor(child.getText());
break;
case "price":
book.setPrice(Double.valueOf(child.getText()));
break;
case "year":
book.setYear(child.getText());
break;
case "language":
book.setLanguage(child.getText());
break;
default:
System.out.println("未知属性!");
}
}
// 向书籍列表添加书籍对象
bookList.add(book);
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 生成XML文件
/**
* 创建XML文件,保存book列表
* @param bookList
* @param fileName
* @param encoding
*/
private void createXML1(List<Book> bookList, String fileName,
String encoding) {
// 1.创建Document文件
Document document = DocumentHelper.createDocument();
// 2.创建root节点
Element root = document.addElement("bookstore");
// 3.创建book节点
for (Book book : bookList) {
Element ebook = root.addElement("book");
// 4.1为book节点添加属性
ebook.addAttribute("id", String.valueOf(book.getId()));
// 4.2为book增加子节点,并设置文本值
Element eTitle = ebook.addElement("title");
eTitle.addCDATA(book.getTitle());
Element eAuthor = ebook.addElement("author");
eAuthor.setText(book.getAuthor());
Element eYear = ebook.addElement("year");
eYear.setText(book.getYear());
Element ePrice = ebook.addElement("ePrice");
ePrice.setText(String.valueOf(book.getPrice()));
Element eLanguage = ebook.addElement("language");
eLanguage.setText(book.getLanguage());
}
// 5.声明输出流
OutputStream output = null;
XMLWriter writer = null;
// 6.设置输出的XML格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 7.设置XML文档编码
format.setEncoding(encoding);
try {
// 8.实例化输出流
output = new FileOutputStream(fileName);
// 9.实例化XML输出流,加载XML格式及设置
writer = new XMLWriter(output, format);
// 10.向硬盘写入XML
writer.write(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != output) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 创建RSS文件
/**
* 创建RSS文件
*
* @param fileName
*/
private void createXML(String fileName) {
// 1.创建document对象,代表XML文件
Document document = DocumentHelper.createDocument();
// 2.创建根节点
Element rss = document.addElement("rss");
// 3.为rss节点添加version属性(版本)
rss.addAttribute("version", "2");
// 3.1生成子节点及节点内容
Element channel = rss.addElement("channel");
// 3.2生成内容
Element title = channel.addElement("title");
title.setText("<![CDATA[国内最新新闻]]>");
// 4.创建xml输出流
File file = new File(fileName);
XMLWriter writer = null;
try {
// 5.设置xml格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置xml编码
format.setEncoding("GBK");
writer = new XMLWriter(new FileOutputStream(file), format);
// 设置是否转义,默认为true(转义)
writer.setEscapeText(false);
// 6.将XML文件写到硬盘
writer.write(document);
} catch (UnsupportedEncodingException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
以上是关于XML文件--使用DOM4J示例的主要内容,如果未能解决你的问题,请参考以下文章