XML and XSLT implementation code generator (i)
Summary
XML and XSLT provide a very good solution for developing web applications, however, their ability is not only limited to web development, in fact, they provide a good model based on meta DATA programming, using XML as metadata And use XSLT to convert it to any desired form or other data. The first part of this article describes how to use Java and XML and XSLT implementation code generator functions. The second part discusses the advantages and insufficient use of this solution, and how to improve the use of the program, this article also shows JAXP and JDOM API usage of.
Introduction
Writing code is a programmer's daily work, usually writing program code is considered to have creative work, which is also a major reason for many people love programming. However, sometimes people have to face repeatedly prepared some simple, other people bored code, the best example is JavaBean in Java, especially in J2EE applications, programmers need to write many JavaBeans and data layers in the web layer (usually Is EJB) Switching data, writing GET for every JavaBean, and the SET method is very mechanical and boring. If these similar work can be done automatically by the machine, the programmer can spend more effort to develop it in creation. The code generator is for this purpose, which is substantially all popular IDEs, or less capabilities containing code generation. For example, the Basic Framework code for generating an MFC when creating a project in the VC, and the function of automatically generating Javadoc comments in the JBuilder High release. To implement a code generator, you must first provide basic information about what code to generate, that is, the metadata, and then the program extracts these metadata and automatically generates the actual code. It is not difficult to imagine that the best way to provide metadata is to use a unified, easy to verify, and easy to extract data format, XML, DTD, or XML Schema (optional), and XSLT combinations. XML provides unified data, DTD or XML Schema to verify the validity of the data, while XSLT is used to extract XML metadata and convert. The rear portion will take a JavaBean code generator as an example, detail how to combine these techniques for practical applications.
Establish a simple model
JavaBean is a simple Java class that contains a set of properties, usually includes field definition, constructor, and a few pairs of Get / SET methods. You can first create a DTD for XML metadata indicating that it should contain basic information. Listing 1.1 shows a such DTD model. XML Version = "1.0" encoding = "UTF-8"?>
Melbourne
University ->
"Attlist property
Type cdata #Requidedset (Yes | NO) #implie
Get (Yes | NO) #implied>
Listing 1.1
JavaBean's IMPLEMENT sub-elements represent the interface implemented by the bean, of course, an extend child element indicates that the class extension is expanded, and the property list of the Property element can also be added to the isIndex property in the future to indicate whether the attribute is an index property. . Since DTD is basically self-explanatory, it will be explained in detail. After the DTD design is complete, you can use it to write XML data. List 1.2 is a simple XML example
XML Version = "1.0" encoding = "UTF-8"?>
package>
Property>
Property>
Property>
Property>
javabean>
xgen>
Listing 1.2