Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Apache Digester Application

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you Apache Digester application sample analysis, I believe most people do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!

Using apache commons-digester to automatically create java objects from xml files

I. order

Digester is a component of apache called apache commons-digester.jar, which makes it easy to generate java objects from xml files. You don't have to read a document object through jdom or Xerces anymore. (jdom and Xerces still have their uses and strengths, and you can't live without them in other apps.)

I'm just going to give you an example of how to generate a java object from an xml file.

First you must add jar to your project classpath.

(i) Preparation:

1. Environment: Eclipse, jdk1.4+

2. Requirements: College contains students and teachers, students can choose courses, teachers have different qualifications.

3. Java Class definition, for simplicity, omitting import content.

4. To generate java objects using Digester, you must define an xml file, which must conform to the rules of digester-rules.dtd.

All xml files must be in the classpath path so that xml files can be read via Class.getClassLoader().getInputStream().

(ii) Summary:

The java class and xml files required for this example fall into four categories:

1. Base xml file: [academy.xml]: xml file you will parse.

2. Base java class: [Academy.java, Teacher.java, Cousre.java]: Class definition of generated object Student.java

3.digesterRules.xml: [academyRules.xml]: defines the matching (mapping) relationship between the base xml file and the base java class

4. Digester class: [DigestXMLJavaAcademy.java]: parse the basic xml file according to digesterRules.xml and generate java objects

(iii) References

1.English ebook: Pro Jakarta Commons by Harshad Oak Apress 2004

II. Basic Java Class

1. College class Academy.java

Java code

2. Student Student.java

Java code

3. Teacher Teacher.java

Java code

4. Courses Course.java

Java code

III. Basic XML file

============================================================

Academy academy.xml, containing students (including courses), teachers

Java code

IV.Digester Class

DigestXMLJavaAcademy.java and academyRules.xml

AcademyRules.xml is used to define the matching (mapping) relationship between xml files and java objects, and the format should conform to the format constraints of digester-rules.dtd.

DigestXMLJavaAcademy.java Parses the academy.xml file according to academyRules.xml and generates Academy objects.

============================================================

Java code

2. DigestXMLJavaAcademy.java

Java code

That's all for the "Apache Digester Application Sample Analysis" article, thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report