In this section we will explain how Advance Shipping Notice (ASN) XML unmarshalling solution is implemented.
The following steps are involved in a Castor unmarshalling procedure:
For example, for this sample we created asn directory with marshal and unmarshal subdirectories. unmarshal directory also contains schema subdirectory where final versions of XML Schema documents are located. Java compiler and ant automatically create additional subdirectories, java, java/sample, java/asn,and lib.
The following XML documents, programs and scripts are located in unmarshal directory:
Later on when ant is executed using build.xml, java, java/sample, java/asn, and lib directories are created and their content is:
database.xml document contains database configuration details. It is used by Castor JDO database component that supports database transactions in two types of environment, client applications and J2EE servers. Make sure that your version of database.xml document contains your database configuration details (host name, port number, database connect string, database user id and password). Our database.xml example belongs to an Oracle database.
There is also schema directory that contains a SQL script asn.sql and XML Schema document copied from the unmarshal directory by ant:
sqlplus userid/password@db_connect_string @asn.sql
where userid and password are the ones you specified in database.xml. database_connect_string is a database connect string specified in Oracle's tnsnames.ora.
We start with XML Schema document creation based on the ASN XML document and its Document Type Definition (DTD). The XML Schema formally defines XML document structure and its elements data types.
The ASN XML Schema document ( asn.xsd) is created for an ASN XML document sample ( asn.xml) and its DTD document ( asn.dtd). The XML Schema is used by the Castor Source Generator to create Java classes for XML mapping.
Now when we have formal XML Schema definition for the ASN XML document (asn.xsd), we execute Castor Source Generator with the XML Schema document as an input. ASN XML software build is performed by ant and we simply run ant at this point. Before you run ant ensure that castor.dir, jdk.dir, and regexp.dir in build.xml have full directory paths that belong to your software configuration and jar file name in classpath for asn_classes must be changed to your Castor version (castor-0.9.4.1.jar is the one we are using).
Also, the CLASSPATH in the Control Panel's System/Environment (Windows NT/2000/XP) should be updated with the following directories if you installed Castor software in c:\:
Castor Source Generator generates all Java classes required for XML mapping. These classes are located in java/asn. They are used in programs for ASN XML unmarshalling and marshalling.
mapping.xml is an XML document used for ASN XML mapping from the ASN Java classes to the ASN database tables.
Add a copy command for mapping.xml to the "copy" target in build.xml:
<copy file="${asn.dir}/mapping.xml" todir="${src.dir}/sample" />
Run ant again and ensure that it completes successfully.
Java program Asn.java unmarshals ASN XML document. The following tables will be populated with data from the document:
build.xml has to be updated to include Asn.java compilation:
Add the following line to "copy" target:
<copy file="${asn.dir}/Asn.java" todir="${src.dir}/sample"/>
Create new target "sample-classes" that will compile Asn.java:
<target name="sample-classes" depends="asn-jar">
<javac srcdir="${src.dir}/sample" destdir="${src.dir}/sample" includes="Asn.java"
classpath="${lib.dir}/asn.jar;${castor.dir}/castor-0.9.4.1.jar;
${castor.dir}/classes12.zip;${jdk.dir}/tools.jar" />
</target>
Change default in build.xml project to "sample-classes".
Ensure that the database name in the following Asn.java statement:
_jdo.setDatabaseName( "ebdb" );
is the one you use. If it is "ebdb" leave it, otherwise change it to your name.
Run ant to compile Asn.java
Start asn.bat script to execute ASN XML unmarshal program. Before you start the script, change all directory paths to ones that belong to your system configuration.
The following steps have to be performed in order to implement marshalling solution:
Marshalling currently requires several additional steps because the current version of Castor (0.9.4.1) does not has full support for Java ArrayList data type. marshal directory should be created and complete unmarshal directory should be copied to it. After that, the content of the marshal directory should be the same as the list below. All unnecessary files should be removed.
After we copy files from the unmarshal directory and remove unnecessary files, the marshal directory will contain the following XML documents, programs, and scripts:
marshal directory will also contain java/sample, java/asn, that are copied from the unmarshal directory. Marshaling does not need schema directory.
build.xml document must be modified by
ArrayList collections are added for AdvanceShippingNotice, Container, and Product in mapping.xml and an additional marshal mapping document marshalmap.xml is created.
All Java classes that contain ArrayList data type:
must be modified and the following code changes have to be done:
Asn.java program will marshal all data from the database and create output.xml document that will have the same content as asn.xml. Save Asn.java in java/sample.
Execute ant to build marshalling example:
ant
Create asn.bat and start the script to execute ASN XML marshal program:
asn
After successful execution, an XML document output.xml will be created in marshal directory.