Use digester to convert XML into Bean

Source: Internet
Author: User

The project needs to use the parsing configuration file function. Google found that digester is basically used. This jar package exists in the project, so you can use it and study it. Digester was originally a jar under struts1.x, but since parsing XML to bean is such a common function, it is split into a separate project of Apache, which is implemented in pure Java. After a try, it is quite convenient to parse XML. The following is an example on the official website, which covers common functions.

 

Example. xml:

 <Address-book> <br/> <person id = "1" Category = "acquaintance" Birth = "1978-10-16"> <br/> <Name> Gonzo </Name> <br/> <email type = "business"> gonzo@muppets.com </Email> <br/> <address> <br/> <type> Home </type> <br/> <street & gt; 123 Maine Ave. </street> <br/> <city> Las Vegas </city> <br/> <State> NV </State> <br/> <zip> 01234 </zip> <br/> <country> USA </country> <br/> </address> <br/> <type> business </type> <br/> <Street> 234 maple dr. </street> <br/> <city> Los Angeles </city> <br/> <State> Ca </State> <br/> <zip> 98765 </zip> <br/> <country> USA </country> <br/> </address> <br/> <Hobbie> <br/> <item> sprots </item> <br/> <item> pets </item> <br/> </Hobbie> <br/> </person> </P> <p> <person id =" 2 "Category =" rolemodel "Birth =" 1958-2-3 "> <br/> <Name> Kermit </Name> <br/> <email type =" business "> kermit@muppets.com </ email> <br/> <email type = "home"> kermie@acme.com </Email> <br/> <address> <br/> <type> business </type> <br /> <Street> 987 brown RD </street> <br/> <city> Las Cruces </city> <br/> <State> nm </State> <br/> <zip> 75321 </zip> <br/> <country> USA </country> <br/> </address> <br/> <Hobbie> <br/> <item> driving </item> <br/> <item> music </item> <br/> </Hobbie> <br/> </person> </P> <p> </address-book>

 

Beans:

Package addressbook; </P> <p> Import Java. util. shortlist; </P> <p> public class addressbook {<br/> shortlist people = new shortlist (); </P> <p> Public void addperson (person P) {<br/> people. addlast (p); <br/>}< br/>}Package addressbook; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class Hobbie {<br/> private list <string> items = new arraylist <string> (); </P> <p> Public void additem (string item) {<br/> items. add (item); <br/>}</P> <p> public list <string> getitems () {<br/> return items; <br/>}< br/>

 

 Package addressbook; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. iterator; <br/> Import Java. util. list; </P> <p>/** <br/> * See main. java. <br/> */<br/> public class person {<br/> private int ID; <br/> private string category; <br/> private string name; <br/> private hashmap <string, string> emails = new hashmap <string, string> (); <br/> private list <address> addresses = new arraylist <address> (); <br/> private Hobbie; <br/> private string birthday; </P> <p>/** <br/> * a unique ID for this person. note that the digester automatically <br/> * converts the ID to an integer. <br/> */<br/> Public void setid (int id) {<br/> This. id = ID; <br/>}</P> <p> Public void setcategory (string category) {<br/> This. category = category; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p>/** <br/> * We assume only one email of each type... <br/> */<br/> Public void addemail (string type, string address) {<br/> emails. put (type, address); <br/>}</P> <p> Public void addaddress (address ADDR) {<br/> addresses. add (ADDR); <br/>}</P> <p> Public void sethobbie (Hobbie) {<br/> This. hobbie = Hobbie; <br/>}</P> <p> Public hashmap <string, string> getemails () {<br/> Return Emails; <br/>}</P> <p> Public void setemails (hashmap <string, string> emails) {<br/> This. emails = emails; <br/>}</P> <p> public list <address> getaddresses () {<br/> return addresses; <br/>}</P> <p> Public void setaddresses (list <address> addresses) {<br/> This. addresses = addresses; <br/>}</P> <p> Public Hobbie gethobbie () {<br/> return Hobbie; <br/>}</P> <p> Public void setbirthday (string birthday) {<br/> This. birthday = birthday; <br/>}< br/>}

 

 

 

Package addressbook; </P> <p> Import Java. util. hashmap; <br/> Import Java. util. iterator; </P> <p> Public Class address {<br/> private string type; <br/> private string Street; <br/> private string city; <br/> private string state; <br/> private string zip; <br/> private string country; </P> <p> Public String GetType () {<br/> return type; <br/>}</P> <p> Public void settype (string type) {<br/> This. type = type; <br/>}</P> <p> Public String getstreet () {<br/> return Street; <br/>}</P> <p> Public void setstreet (string Street) {<br/> This. street = Street; <br/>}</P> <p> Public String getcity () {<br/> return city; <br/>}</P> <p> Public void setcity (string city) {<br/> This. city = city; <br/>}</P> <p> Public String getstate () {<br/> return state; <br/>}</P> <p> Public void setstate (string state) {<br/> This. state = State; <br/>}</P> <p> Public String getzip () {<br/> return zip; <br/>}</P> <p> Public void setzip (string zip) {<br/> this.zip = zip; <br/>}</P> <p> Public String getcountry () {<br/> return country; <br/>}</P> <p> Public void setcountry (string country) {<br/> This. country = country; <br/>}< br/>}

 

 

 

Test class:

Package addressbook; </P> <p> Import Org. apache. commons. digester. digester; </P> <p> public class main {<br/> Public static void main (string [] ARGs) throws exception {<br/> digester d = new digester (); <br/> addressbook book = new addressbook (); <br/> D. push (book); <br/> addrules (d); <br/> D. parse (main. class. getresource ("/addressbook/example. XML "); <br/> system. out. println ("Bye bye"); <br/>}</P> <p> Private Static void addrules (digester d) {</P> <p> D. addobjectcreate ("address-book/person", person. class); </P> <p> // creates a person object <br/> D. addsetproperties ("address-book/person"); <br/> // Add this person to addressbook <br/> D. addsetnext ("address-book/person", "addperson"); </P> <p> // read name <br/> D. addcallmethod ("address-book/person/Name", "setname", 0); <br/> // D. addbeanpropertysetter ("address-book/person/Name"); </P> <p> // Add email <br/> D. addcallmethod ("address-book/person/email", "addemail", 2); <br/> D. addcallparam ("address-book/person/email", 0, "type"); <br/> D. addcallparam ("address-book/person/email", 1); </P> <p> // Add address object <br/> D. addobjectcreate ("address-book/person/address", address. class); <br/> D. addsetnext ("address-book/person/address", "addaddress"); <br/> D. addsetnestedproperties ("address-book/person/address"); </P> <p> // Add a hobbie object <br/> D. addobjectcreate ("address-book/person/Hobbie", hobbie. class); <br/> D. addsetnext ("address-book/person/Hobbie", "sethobbie"); <br/> D. addcallmethod ("address-book/person/Hobbie/item", "additem", 0); <br/>}< br/>}

 

 

Briefly,

1) when creating bean attributes, such as the person object of addressbook, it always takes two steps:

Addobjectcreate

Addsetnext

The former tells the system that the elements below will be mapped into an object, and the latter tells the system which method of the current parent object is used to add this new sub-object, which cannot be replaced by addcallmethod, if multiple elements are used, only one element is added. I do not know the reason and the document is not clear.

 

2) read attribute

Two Methods: addsetproperties (). It uses the attribute in XML to set the property with the same name on the bean. What should we do if the two names are different, for example, birth in XML, the bean is called birthday. The second method is addsetproperties ("address-book/person", "birth", "Birthday ");

 

3) set the element text to the bean property.

I generally prefer addbeanpropertysetter and addcallmethod. However, to specify the number of parameters, if there is only one parameter, the number is expressed as 0, a little weird.

 

In addition, the addsetnestedproperties method can set a group of element button element names to bean properties, such:

<Address> <br/> <type> business </type> <br/> <Street> 234 maple dr. </street> <br/> <city> Los Angeles </city> <br/> <State> Ca </State> <br/> <zip> 98765 </zip> <br/> <country> USA </country> <br/> </address>

 

Use addsetnestedproperties ("address-book/person/address") to set these child elements as property to address.

 

If you want to set a constant attribute for bean, use addobjectparam after addcallmethod. Digester is hard at all, and you will understand it when you do more hands-on exercises.

 

Is it easy? After learning about the principle, when the XML structure changes, you can simply modify the code and synchronize it. In addition, there seems to be a way to use xmlbeans and implement it with annotation. I have seen it in a project, but I have not studied it.

 

Official example:

Http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/examples/

 

Other jar packages required, commons-logging, beanutils

To use the log, define a commons-logging.properties file with the following content:

Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. simplelog
Org.apache.commons.logging.simplelog.log.org. Apache. commons. digester. digester = debug
Org.apache.commons.logging.simplelog.log.org. Apache. commons. digester. digester. Sax = info

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.