利用Digester对XML文件进行解析
以前解析XML文件主要是使用JDOM,最近研究Struts和iBatis的代码,发现他们都是使用了Digester这种快速将XML转化为Object的XML处理组件。于是,我便花了一点时间找了一个Digester的Demo。下面是代码:
package com.example.demo.domain;
public class Article {
private String headline;
private String page;
public Article() {
}
public void setHeadline(String rhs) {
headline = rhs;
}
public void setPage(String rhs) {
page = rhs;
}
public String toString() {
return "Article:
Headline='" + headline + "' on
page='" + page + "' ";
}
}
package com.example.demo.domain;
public class Book {
private String author;
private String title;
public Book() {
}
public void setAuthor(String rhs) {
author = rhs;
}
public void setTitle(String rhs) {
title = rhs;
}
public String toString() {
return "Book:
Author='" + author + "'
Title='" + title + "'";
}
}
package com.example.demo.domain;
import java.util.Vector;
public class Catalog {
private Vector books;
private Vector magazines;
public Catalog() {
books = new Vector();
magazines = new Vector();
}
public void addBook(Book rhs) {
books.addElement(rhs);
}
public void addMagazine(Magazine rhs) {
magazines.addElement(rhs);
}
public String toString() {
String newline = System.getProperty("line.separator");
StringBuffer buf = new StringBuffer();
buf.append("--- Books
---").append(newline);
for (int i = 0; i < books.size(); i++) {
buf.append(books.elementAt(i)).append(newline);
}
buf.append("---
Magazines ---").append(newline);
for (int i = 0; i < magazines.size(); i++) {
buf.append(magazines.elementAt(i)).append(newline);
}
return buf.toString();
}
}
package com.example.demo.domain;
import java.util.Vector;
public class Magazine {
private String name;
private Vector articles;
public Magazine() {
articles = new Vector();
}
public void setName(String rhs) {
name = rhs;
}
public void addArticle(Article a) {
articles.addElement(a);
}
public String toString() {
StringBuffer buf = new StringBuffer("Magazine: Name='" + name + "' ");
for (int i = 0; i < articles.size(); i++) {
buf.append(articles.elementAt(i).toString());
}
return buf.toString();
}
}
上面是4个domain对象。下面是处理解析对象。
package com.example.demo.digester;
import java.io.File;
import org.apache.commons.digester.Digester;
import com.example.demo.domain.Article;
import com.example.demo.domain.Book;
import com.example.demo.domain.Catalog;
import com.example.demo.domain.Magazine;
public class DigesterDriver {
public static void main(String[] args) {
try {
Digester digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate("catalog", Catalog.class);
digester.addObjectCreate("catalog/book", Book.class);
digester.addBeanPropertySetter("catalog/book/author", "author");
digester.addBeanPropertySetter("catalog/book/title", "title");
digester.addSetNext("catalog/book", "addBook");
digester.addObjectCreate("catalog/magazine", Magazine.class);
digester.addBeanPropertySetter("catalog/magazine/name", "name");
digester.addObjectCreate("catalog/magazine/article", Article.class);
digester.addSetProperties("catalog/magazine/article", "page", "page");
digester.addBeanPropertySetter("catalog/magazine/article/headline");
digester.addSetNext("catalog/magazine/article", "addArticle");
digester.addSetNext("catalog/magazine", "addMagazine");
File input = new File("E:\\MyProjects\\Workspace\\DigesterDemo\\src\\com\\example\\demo\\digester\\example.xml");
Catalog
c = (Catalog)
digester.parse(input);
System.out.println(c.toString());
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
下面是XML文件:
<?xml version="1.0"?>
<catalog library="somewhere">
<book>
<author>Author 1</author>
<title>Title 1</title>
</book>
<book>
<author>Author 2</author>
<title>His One Book</title>
</book>
<magazine>
<name>Mag Title 1</name>
<article page="5">
<headline>Some Headline</headline>
</article>
<article page="9">
<headline>Another Headline</headline>
</article>
</magazine>
<book>
<author>Author 2</author>
<title>His Other
Book</title>
</book>
<magazine>
<name>Mag Title 2</name>
<article page="17">
<headline>Second Headline</headline>
</article>
</magazine>
</catalog>