Simplifying XML document processing with Digester (1)

Source: Internet
Author: User
Tags tostring stringbuffer
The XML digester framework belongs to the Jakarta Commons, which processes XML documents based on rules and schemas. Compared to standard APIs such as sax and DOM, Digester does not involve too many details, and is ideal for simple processing of XML documents.
In Java and XML development, a common task is to transform an XML document into a hierarchical structure of corresponding Java bean objects. People often use standard sax and Dom APIs to accomplish this task. While both of these APIs are powerful and flexible, they appear to be too low-level for some simple tasks, that is, too many detail issues involved. The Jakarta digester framework is well adapted to the needs of such situations.
Introduction to the Digester framework

Jakarta's Digester framework, developed from the Struts framework, was originally used to process struts-config.xml configuration files, but it was soon recognized that it had a wider use and was transferred to the Jakarta Commons project. The goal of Jakarta Commons is to provide a "repository of reusable Java components". The latest version of Digester is 1.3, released on August 13, 2002.
The Digester framework allows developers to specify a set of actions that are executed when the parser discovers certain simple patterns in the XML document. The Digester framework has 10 predefined rules (rule) that cover most of the requirements for unmarshalling XML (such as creating beans or setting bean properties) (marshalling is intended to mean "neat, marshalling trains", Marshalling is the process of generating XML description documents for Java objects in memory, and unmarshalling refers to the process of translating XML form descriptions into objects that can be manipulated by Java code, which we call "reverse compounding". However, users can define and implement their own rules when necessary.
In the example of this article, we will reverse the configuration of the following XML document:
<?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" >
</article>
<article page= "9" >
</article>
</magazine>
<book>
<author>author 2</author>
<title>his other book</title>
</book>
<magazine>
<name>mag Title 2</name>
<article page= ">"
</article>
</magazine>
</catalog>

The following is the code for the bean. Note When you use the Digester framework, the Bean class must be defined as public.
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 ();
}
}
//===================================================
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 + ""
}
}
//===================================================
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 ();
}
}
//===================================================
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 + ""
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.