沉睡森林@漂在北京

本处文章除注明“转载”外均为原创,转载请注明出处。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  152 随笔 :: 4 文章 :: 114 评论 :: 0 Trackbacks

利用DigesterXML文件进行解析

 

以前解析XML文件主要是使用JDOM,最近研究StrutsiBatis的代码,发现他们都是使用了Digester这种快速将XML转化为ObjectXML处理组件。于是,我便花了一点时间找了一个DigesterDemo。下面是代码:

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();

 

    }

}

 

上面是4domain对象。下面是处理解析对象。

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>

posted on 2008-10-16 10:46 王总兵 阅读(264) 评论(0)  编辑  收藏 所属分类: Digester

只有注册用户登录后才能发表评论。


网站导航: