Implement castor data binding — turn to

Part 1: installing and setting up castor

In XML newsgroups In the discussion forums of mailing lists and websites (where you can find links to these contents), one of the most common topics is data binding. Java and XML developers are looking for a simple way to convert Java objects and XML documents back and forth. Sun uses its JAXB, Java architecture for XML binding (if you see the acronym JAXB elsewhere, that's normal; sun seems to change the meaning of JAXB every year), it dominates the field of data binding. However, JAXB API (if you like, it can also be called schema) has some shortcomings and the update speed is slow. It can't handle the mapping to relational database, which is a common request.

It was in this case that castor appeared. Castor is an open source framework that can be used in areas where JAXB cannot be used. Castor has been developing and predates the JAXB code base and sun data binding specifications. In fact, castor has been updated to implement data binding in combination with JAXB methods, so programmers using JAXB can easily move code.

Before discussing the details of installing and using castor, it is necessary to point out the reasons for trying castor and moving from JAXB to castor. First, castor is almost a replacement for JAXB. let me put it another way, You can easily turn all JAXB code into castor (not completely replaced, but enough to make it easy for programmers who have just come into contact with castor). Secondly, castor provides many functions in the field of data binding, which can convert between Java and XML without using schema, provide a binding schema that is easier to use than JAXB, and can group relational databases and XML documents (Marshall) reconciliation group (unmarshal). Castor also provides the JDO function. JDO, that is, Java data objects, is the underlying technology that drives java to RDBMS marshalling and unmarshalling. Although it is no longer as popular as in previous years, JDO is still a good function. In addition, because JDO is also a sun specification, there is no need to write a fuzzy API. The installation process of castor is very simple. First, access Castor web site (see the link in) and click download in the left menu. Select latest milestone release, and then scroll down to download sets. You can download castor jar, DTD, Doc, dependency, etc., and the pre packaged download suite is very easy to use (see Figure 1). In this article, we will use version 1.1.2.1. I choose the castor jars, docs, DTDs, command line tools, and examples download suite in ZIP format. You will get an expandable archive file containing many jar files, documents, and sample programs (see Figure 2). Next, you need to put all castor files in the correct location in the system so that your Java environment can access them. I strongly recommend that all third-party Java libraries be placed in a common location. You can put them anywhere in the system, but doing so will have serious consequences because it is difficult to find them in most cases Get what you need. You will spend a lot of time determining the library version to use, because you will often put multiple versions in different locations on the system. The classpath will become long and difficult to understand. I put all my libraries in / usr / local / Java /, Each library is placed in its own subdirectory (each directory usually has a version number). Therefore, move the castor Archive - expanded - to your common library location. In this case, the full path of castor is: / usr / local / Java / castor-1.1.2.1. Another step in setting up Java libraries in the system is to locate and link to documents. You often do this, and most Java libraries provide documents Local copy of (including Javadoc) in HTML format. In castor, this path is castor-1.1.2.1/doc/. Therefore, in my system, I added a bookmark for / usr / local / Java / castor-1.1.2.1/doc/index.html. Figure 3 shows the appearance of locally loaded castor documents, version 1.1.2.1. These operations are required in this article and in your daily programming There are two reasons: the document is local. Have you written a program on the plane? Is there no network link? Can't sign in to Starbucks WiFi? In addition to faster access, local documents also play an important role in these situations. Local documents always fit your needs. As castor evolves, you may not often download the latest distributions. Using online documents means using the latest version of the document, which may not match the version in your system. When using a local document, the document used always corresponds to the currently used library version. Therefore, there will be no confusion and frustration due to the use of inappropriate or non-existent features in the library version. Castor has many dependencies: Apache ant Jakarta commons JDBC 2.0 standard extensions log4j login utility Apache Xerces XML parser, which is actually a compact collection for most castor operations. If you want to build castor from scratch, run these examples, use these tests, or study castor more deeply, you need more dependencies. These dependencies must be placed in the classpath before castor can be used. The cumbersome way to run castor is to first visit castor's download page and note down the version of each dependency you need. Then jump to the web site of each dependency, find the version, download it and add it to the classpath. This method takes a long time, but it is easier to control. In addition, if you have set up and run most libraries properly, this is still a feasible method. Fortunately, there is a better way. Go back to the download page of castor, find the stable version and locate another download package called full SVN snapshot: all sources, and 3rd party libraries (big). Although marked "big", But it's only 10 MB (it's nothing for users who surf the Internet with DSL or cable). Download this file and expand it (as shown in Figure 4). Now you can enter the Lib / directory, which contains a large number of jar files. These are the libraries required by castor. Create a new directory - in the original castor installation directory or a directory of the same level - and then move all the jar files just downloaded to this directory. For example, here, I created it in the castor folder A lib / directory to which all jar files are moved for use by castor. Now you need to set everything in the classpath. I use one in my Mac OS X configuration The profile file handles all these issues. You may want to set your profile to this, or set a system environment variable in windows. In any case, you need to add the following jar file to the classpath: castor-1.1 2.1. Jar (in castor home directory) castor-1.1.2.1-xml.jar (in castor home directory) xerces-j-1.4.0.jar (in the same location as castor dependency Library) commons-logging-1.1.jar (placed in the same location as castor dependency Library) for reference, here is my. Profile. You can see how I set it: export castor_home = $java_base / castor-1.1.2.1 export editor = VI export castor_classes = $castor_home / castor-1.1.2.1.jar: $castor_home / castor-1.1.2.1-xml.jar: $castor_home / lib / xerces-j_1.4.0.jar: $castor_home / lib / commo ns-logging-1.1. jar export CVS_ RSH=ssh export PS1="[whoami:\w] " export CLASSPATH=$XALAN_ HOME/xalan. jar:$XALAN_ HOME/xml-apis. jar: $XALAN_ HOME/xercesImpl. jar: ~/lib/mclaughlin-xml. jar:$CASTOR_ CLASSES:. please make sure that all these files are placed in the classpath. Next, we will do a quick test. First build a very simple class, and then build a utility to convert it back and forth between XML and Java. This is not a demonstration of all the features of castor, but a very basic test. Listing 1 shows a CD class that I will use. Enter these source codes and save them on CD Java (or download these codes from). Import java.util.arraylist; import java.util.list; / * a class to representative CdS / public class CD implements java.io.serializable { /* The name of the CD / private String name = null; /* The artist of the CD / private String artist = null; /* Track listings / private List tracks = null; /* required no-args constructor / public CD() { super(); } /* Create a new CD / public CD(String name,String artist) { super(); this.name = name; this.artist = artist; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setArtist(String artist) { this.artist = artist; } public String getArtist() { return artist; } public void setTracks(List tracks) { this.tracks = tracks; } public List getTracks() { return tracks; } public void addTrack(String trackName) { if (tracks == null) { tracks = new ArrayList(); } tracks. add(trackName); } } Now you need a class to handle marshalling. As shown in Listing 2. import java. io. FileWriter; import org. exolab. castor. xml. Marshaller; public class MarshalTester { public static void main(String[] args) { try { CD sessions = new CD("Sessions for Robert J","Eric Clapton"); sessions.addTrack("Little Queen of Spades"); sessions.addTrack("Terraplane Blues"); FileWriter writer = new FileWriter("cds.xml");

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>