Use digester to convert XML into Java objects

Source: Internet
Author: User

In a relatively complete application system, some configuration files are often required. You can use the. properties file for simple properties, but you should consider using XML files to configure complex objects. The sdks used to read XML files include Dom, sax, and JDOM. However, we all know that these sdks are relatively low-level APIs.CodeLarge, and if the XML file format is modified, the Code must be significantly modified. The digester package in the Jakarta Commons project can easily convert XML files to Java objects. See the following example.

In a project, you need to provide some statistical charts, but the contents of the charts are not yet determined. Therefore, I decided to allow the graph to be configured. All the definitions are saved in a file named charts. in an XML (or an international file name such as charts_zh_cn.xml, only the default language is considered here) file, part of the file is as follows:

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Charts  >      <  Chart  ID = "Barchart1"   >          <  Title  > Figure 1 </  Title  >          <  Legendvisible  > False </  Legendvisible  >          <  Tooltipsvisible > True </  Tooltipsvisible  >          <  Type  > Bar </  Type  >          <  Labelx  > Time </  Labelx  >         <  Labely  > Data </  Labely  >          <  Width  > 500 </  Width  >          <  Height  > 360 </ Height  >          <  Hql  > Select count (C), C. Department. name from Edu. PKU. Pub. Aims. model. Business. Client C group by C. Department </  Hql  >          <  Description  > </  Description  >      </  Chart >  </  Charts  > 

We can see that I have defined attributes such as ID, title, and legendvisible for each graph. These attributes have obvious meanings and will affect the data and performance of the graph on the page. InProgramIn this file, I need to read the definition in this file into a registry class chartregistry, which maintains a registry variable of the Java. util. List type, where each element is a chartconfig class. Now digester should show its value.

To make it easy to use digester, we make chartconfig also have each attribute of the Statistical Chart (ID, title, legendvisible, etc.), name and charts. the attributes (child elements) of the elements in XML correspond one by one, and both have the getter and setter methods. That is to say, chartconfig is a bean class. Defines a deregister () method in the chartregistry class. It is used to read and parse the specified XML file using digester. The Code is as follows. There is also a register () method to add the chartconfig object to the Registry.

 Public   Void Deregister (URL) Throws Ioexception, saxexception {inputstream is = New  Fileinputstream (URL. GetFile (); digester Digester = New  Digester (); digester. Push (  This  ); Digester. setvalidating (  False  ); Digester. addobjectcreate ( "Charts/Chart", chartconfig. Class  ); Digester. addsetproperties ( "Charts/Chart" ); Digester. addbeanpropertysetter ( "Charts/chart/legendvisible" ); Digester. addbeanpropertysetter ( "Charts/chart/tooltipsvisible" ); Digester. addbeanpropertysetter ( "Charts/chart/Title" ); Digester. addbeanpropertysetter ( "Charts/chart/type" ); Digester. addbeanpropertysetter ( "Charts/chart/labelx" ); Digester. addbeanpropertysetter ( "Charts/chart/labely" ); Digester. addbeanpropertysetter ( "Charts/chart/width" ); Digester. addbeanpropertysetter ( "Charts/chart/height" ); Digester. addbeanpropertysetter ( "Charts/chart/hql" ); Digester. addbeanpropertysetter ( "Charts/chart/description" ); Digester. addsetnext ( "Charts/Chart", "register" ); Digester. parse (is); collections. Sort (Registry );} 

Basically, the process of parsing XML by Digester and sax is similar. The principle of digester is to develop rules to check whether matching rules exist when traversing each node, if yes, perform the corresponding operation. For example, in the above Code, "digester. addobjectcreate ("charts/Chart", chartconfig. class); "tells digester: If a node in the form of" charts/Chart "is matched, an" Object creation "operation will be executed. What object will be created, the class should be created as "chartconfig. class Object. Similarly, addsetproperties () tells digester to map all the attributes of the specified node to the attributes of the object. In this example, it refers to the ID attribute; addbeanpropertysetter () is to convert the child node to the object's attributes. This method can also have a second parameter. When the object's attribute name and the child node name are different, it is used to specify the object's attribute name; addsetnext () after a matching node is encountered, a method is executed for the parent object of the current object. The parameter is the current parameter. In this example, chartconfig is executed. register (char Tconfig) method. Therefore, the constructed digester converts each element in charts. xml into a chartconfig object and register it into chartregistry.

with the chartregister object, I can construct a Statistical Chart Based on its content in the program (the Statistical Chart is generally generated using jfreechart, so I will not go into details here ). There are still many tool kits with similar functions as digester, such as caster and Jato. I have never actually used them, but I am very satisfied with other Jakarta projects I have used (such as beanutils, httpclient, brand effect ?), So we chose digester at the beginning: simple and convenient.

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.