An example of using Castor to unmarshal and marshall schemas with
namespaces. - afc@aber.ac.uk 15/12/2004


You'll need to have Castor installed from http://castor.exolab.org/. I
had version 0.9.6-RC2. You also need Xerces for Java from
http://xml.apache.org/. I have Xerces-J 2.6.2. You might also need
Jakarta Commons Logging. I have commons-logging-1.0.4.

First set your CLASSPATH. It should contain the Xerces jars, Castor
jars, and the current working directory (the directory where the tars
in this tarfile untarred to).
For example, mine is:
/home/afc/tools/xerces-2_6_2/xercesImpl.jar:/home/afc/tools/xerces-2_6_2/xercesSamples.jar:/home/afc/tools/xerces-2_6_2/xml-apis.jar:/home/afc/tools/xerces-2_6_2/xmlParserAPIs.jar:.:/usr/local/apache-ant-1.6.2/lib/ant.jar:/home/afc/tools/castor-0.9.6-RC2/castor-0.9.6-RC2.jar:/home/afc/tools/castor-0.9.6-RC2/castor-0.9.6-RC2-xml.jar:/home/afc/tools/castor-0.9.6-RC2/castor-srcgen-ant-task.jar:/home/afc/tools/castor-0.9.6-RC2/jdbc-se2.0.jar:/home/afc/tools/castor-0.9.6-RC2/jta1.0.1.jar:/home/afc/tools/commons-logging-1.0.4/commons-logging-api.jar:/home/afc/tools/commons-logging-1.0.4/commons-logging.jar

Then we run the SourceGenerator. This reads the schemas (.xsd file)
and produces Java classes to implement this schema in the directory
given by the -package option.

java org.exolab.castor.builder.SourceGenerator -i product.xsd -package product
java org.exolab.castor.builder.SourceGenerator -i person.xsd -package person
java org.exolab.castor.builder.SourceGenerator -i company.xsd -package company

Now you should have 3 new directories ("packages") each with some Java
code in them. The namespace to Java package mapping is described in
the castorbuilder.properties file, and this is all Castor/Java needs
to know how to to make and compile the three interdependent
directories.

Now compile the generated Java files:

javac person/*.java
javac product/*.java
javac company/*.java

Now compile and run the makeCompanyFile.java. This program uses the
generated java classes to unmarshall an xml file into memory, add some
more people and products to the company, and write out the results to
the file "newcompany.xml".

javac makeCompanyFile.java
java makeCompanyFile


Your newcompany.xml file should now look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Company
    xsi:schemaLocation="http://users.aber.ac.uk/afc/company company.xsd
         http://users.aber.ac.uk/afc/person person.xsd
         http://users.aber.ac.uk/afc/product product.xsd"
    xmlns:per="http://users.aber.ac.uk/afc/person"
    xmlns:pro="http://users.aber.ac.uk/afc/product"
    xmlns="http://users.aber.ac.uk/afc/company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Person>
        <per:Firstname>John</per:Firstname>
        <per:Surname>Doe</per:Surname>
    </Person>
    <Person>
        <per:Firstname>Fred</per:Firstname>
        <per:Surname>Jones</per:Surname>
    </Person>
    <Person>
        <per:Firstname>Mary</per:Firstname>
        <per:Surname>Smith</per:Surname>
    </Person>
    <Product>
        <pro:Type>Widget</pro:Type>
    </Product>
    <Product>
        <pro:Type>spoon</pro:Type>
    </Product>
</Company>

