Apache Jakarta Commons Digester usage

Source: Internet
Author: User
Tags stringbuffer
XML has become an indispensable technology in our development process. We can use it to describe the configuration of the program, adapt to different data formats, and even use it as a database.
There are a lot of tools to help with XML, and they make it easier for us to live. Jakarta Commons under the digester is a good tool. It provides a convenient way to map XML to Java objects. This may make beginners more confused, for example.
We have such an XML file: 1.        2.      <!-- memos_ 2004-01-16.xml --> 3.      <?xml version= "1.0"?> 4.       <memos> 5.      <memo> 6.           <title>About Jakatar commons Digester</title> 7.           <date>2004-01-16 02:05</date> 8.           <body>we are working on digester 1.6-dev. Because it supported read 9.                attributes of element. we should pay attention to the releasing of  10.            Digester  forIts changing.</body> 11.   </memo> 12.      <memo> 14. <title>ah ha!      It ' s good night!</title> 15.      <date>2004-01-16 4:19</date> 16.           <body>i has enhanced the basic framework of my app. with improved 17. Architecture and performance. It ' s time to sleep.           Good night, 18.  boys.</body> 19.  </memo> 20. </memos> This is my memo, I use XML as "database". Now we want to use the Java program to read/write this "database" what should be done. Ah. Yes, SAX, DOM ..., well, forget about them. We have a more easy way.
Let's start by creating a Memo class that is used to hold a Memo. It has three properties to hold the title, date, and the body of the Memo.        We provided the public Setter and Getter, and we overloaded the toString () method to see Run result 1. 2./* Memo.java * * 3. PackageKitta.memo; 4.1. Public classMemo 2. {3./*-----/instance Field (s)/------------------------------------------------/* 4. Private String_title; 5. Private String_body; 6. Private String_date; 7./*-----/constructor (s)/---------------------------------------------------/* 8. PublicMemo () 9.      {* * do no now */11.   } 12. /*-----/getter (s) & Setter (s)/--------------------------------------------/14. Public StringGetBody () 15. {16. return_body;   17.} 18. 19. Public voidSetbody ( StringBody) 20. {_body = body;   22.} 23. 24. Public StringGetTitle () 25. {26. return_title;   27.} 28. 29. Public voidSettitle ( StringTitle) 30. {_title = title;   32.} 33. 34. Public StringGetDate () 35. {36. return_date; 37.} 38. Public voidSetdate ( StringDate) 39. {_date = date;     41.} 42. /*-----/overrided Method (s)/----------------------------------------------/43. Public StringToString () 44. {45. StringBufferBUF = New StringBuffer(); Buf.append ("/t"). Append (_title); Buf.append ("/tat/t"). Append (_date). Append ("/n/n"); Buf.append ("/t"). Append (_body). Append ("/n"); Buf.append ("-----------------------------------------------------------/n"); 50. returnBuf.tostring ();  51.} 52. And then the memos class, which is actually a collection of Memo objects that can be substituted for it with a Collection subclass, but it is used primarily for conceptual clarity. It is also very simple, a private property _memos is used to hold an instance of all Memo objects, a common method Addmemo () is used to add Memo, the ToString () method to the same purpose. 1./* Memos.java * * 2. PackageKitta.memo; 3.4. ImportJava.util. Collection; 5. ImportJava.util. iterator; 6. ImportJava.util. Vector;        7.8. 9. Public classMemos 10.

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.