A simple example
Assume that there are two JavaBeans as follows, Foo and Bar, respectively.
package mypackage; public class Foo {public void addBar (Bar bar); public Bar findBar (int id); public Iterator getBars (); public String getName (); public void setName (String name);} public mypackage; public class Bar {Public int getId (); public void setid (int ID); public string gettitle (); public void settitle (String Title);}
Configure with the XML file below
Use the following lines to complete the configuration file resolution work:
Digest analysis code
Comment
Digester digester = new digester ();
Digester.SetValidating (false);
Not performed the legality verification of XML and the corresponding DTD
Digester.addObjectCreate ("foo", "mypackage.foo");
Create a MyPackage.foo object when you encounter
Digester.AddSetProperties ("foo");
Set the properties of the Foo object that is created according to the attributes of the
Digester.adDObjectcreate ("Foo / Bar", "Mypackage.bar");
Create a MyPackage.bar object when you encounter
Digester.AddSetProperties ("Foo / Bar");
Set the properties of the BAR object that is created according to the attributes of the
Digester.AddSetNext ("Foo / Bar", "Addbar", "MyPackage.bar");
When you encounter the child element
Foo foo = (foo) digester.parse ();
After the analysis, return the root element.
Examples in digester package
****************** ********
Public void addemail (String Type, string address) {emails.put (type, address);} public void print () {system.out.println ("Person #" ID); system.out.println ("category = " Category; system.out.println (" name = " name); for (Iterator i = emails.keyset (). Iterator (); I.hasNext ();) {String Type = (String) i. NEXT (); string address = (string) emails.get (type); system.out.println ("Email (Type"): " address);}} ******** ******************* Import java.util.LinkedList; import java.util.ITerator; public class addressbook {linkedList people = new linkedList (); public void addperson (Person P) { People.addlast (P); PUBLIC VOID Print () {System.out.Println ("Address Book Has" People.Size () "Entries");
For (iTerator i = people.iterator (); I.hasNext ();) {Person P = (Person) i.next (); p.Print ();}}} ********** ** AddressBookDigester ********* import org.apache.commons.digester.Digester; / ** * Usage: java Example1 example.xml * / public class AddressBookDigester {public static void main (String [] args) {If (args.length! = 1) {usage (); system.exit (-1);} String filename = args [0]; // Create a Digester instance Digester D = new digester (); // Create AddressBook Example and press it into the top of the stack. AddressBook Book = New AddressBook (); D.Push (Book); // Add Rule AddRules (d); // Processing Enter XML file try {java.io.file srcfile = new java.io.file (filename); D.PARSE (Srcfile);} catch (java.io.Exception} {system.out.println ("Error Reading Input" IoE.GetMessage ()); System.exit (-1);} catch Org.xml.sax.saxexception se) {system.out.println ("Error Parsing Input" se.getMessage ()); system.exit (-1);} // Print the resolution address data Book.print ();} private static void addrules (Digester D) {// When you encounter
D. ADSETPROPERTIES ("Address-Book / Person"); // Call the AddPerson method of the second stack of top objects (AddressBook instance), with the object of the stack object (Person instance) as the parameter d.AddSetNext ("Address-Book / Person "," Addperson "); // When encountering the child element
Private static void usage () {system.out.println ("usage: java example1 example);}}
The results of the operation are as follows (XML-Crimson, which may require XML-CRIMSON, one source Sun's XML parser, can go to http://xml.apache.org/crimson/ download)
Address book has 2 entriesPerson # 1 category = acquaintance name = Gonzo email (type business): gonzo@muppets.comPerson # 2 category = rolemodel name = Kermit email (type business): kermit@muppets.com email (type home): kermie @ ACME.COM