This article supporting source code
What you should have now (review)
As in the previous article, this article also makes some assumptions about your system setup and your skills. First, you need to download and install the latest version of Castor as described in part 1th of this series, setting the CLASSPATH and related Java libraries. Then, in accordance with the description in part 2nd, familiarize yourself with the basic marshalling and reconciliation group facility of Castor.
Therefore, you should be able to use Castor to extract data from an XML document and work with your own Java class to process the data. In the case of data binding terms, this is called the Solution Group (unmarshalling). The reverse process is called marshalling (marshalling): You should be able to convert the data stored in a member variable of a Java class into an XML document. If you are not familiar with Castor and marshalling, you should read part 2nd (see Resources for links).
Data binding in Non-ideal environments
At first glance, you seem to have mastered all the processes needed to effectively use Castor: setting, grouping, and reconciliation groups. But everything you've learned so far applies only to the so-called ideal environment. In such an environment, the XML written by everyone is perfect, and the element names are meaningful, such as "title" and "Authoraddress", not "T" or "AA". Java classes are created in an organized fashion, using the singular as the class name (such as "book"), using a single number as the name of the member variable (such as "ISBN" and "Price"). In addition, the data type is correct: No developer sets the data type of price to int instead of float, or uses a char array to store string data (this is the practice of C language).
However, most programmers are not in a perfect environment (I really want to find a magical wardrobe that will send me to the perfect World). There are many undesirable situations in most programmers ' environments: XML documents often have bad element and attribute names, as well as namespace problems. element data is stored in attributes, and some data is even delimited by a pipe character or a semicolon.
Java classes are inherited, and the cost of organizing them over time and effort may outweigh the benefits. These classes are often not easily mapped to XML schemas (the idea that XML and data people will compromise with Java programmers is also very incredible), and in some cases, even if you implement a simple mapping, you certainly don't cross all classes and data. XML element names may not be appropriate, and many Java variable names may not be appropriate. You may even encounter names that use the Hungarian notation, in which all member variables begin with "M", such as Mtitle. It's not very nice.
In these cases, the data-binding methods you have learned are powerless. Hungarian-style element names may appear in XML documents, and meaningless structures may also appear in Java classes. This situation is unacceptable. If you can't get and manipulate the data in an XML document the way you want it to, what's the point of Castor (or any data-binding framework)?
The goal of flexible data binding
The first thing to note is that in Castor or any other data-binding framework, it takes some time to use the mapping file. You must first learn some new syntax. Although the mapping file uses XML format (most frameworks do), you need to learn some new elements and attributes. You also have to do some testing to make sure that the conversion between XML and Java code produces the results you want. Finally, if you specify the mappings yourself, rather than having the framework process the mappings, you may encounter more errors in the data binding. For example, if you want the framework to map the fiddler elements in XML to the violin attribute in Java code, but incorrectly declare that the property is in the Player class (which should be in the Player Class), you will encounter an error. Therefore, when you specify a mapping yourself, you must pay great attention to spelling, capitalization, underlining, single quotes, and double quotes.
Before you learn to use a mapping file, you should make sure that you do. If you have the mapping file, but do not use it, it is a waste of time. However, mappings do have some advantages.
Java code is no longer limited by the way XML is named
As mentioned earlier, capitalization can cause errors when you convert XML to Java code. In XML, the most common practice is to name all lowercase and add hyphens, such as first-name. Sometimes, you can even see first_name. Such a name translates to a hard to read Java property name; no one wants to call Getfirst-name () in code. In fact, in most documents written by programmers (rather than XML developers or data administrators), the Hump style (camel-case) nomenclature, such as firstName, is often used. By using the mapping file, it is easy to map XML-style names (such as First-name) to Java-style names (such as FirstName). The best part is that you don't have to force an XML person to think like a Java programmer, which is often more difficult than learning a new mapping syntax.