Tutorials : Converting XML documents to Java objects with Castor XML :

Castor tools and Schemas

If your starting point is the XML-document, and you don't have any Java classes yet to handle this document, then Castor has tools that can generate the classes. These classes will work without a mapping file, as we'll see shortly. 

For Castor to create the Java classes it needs an XML Schema file. If you only have a DTD then Castor has a tool that'll convert a DTD to a Schema file. And if you have neither a Schema nor a DTD file but only an example of how your XML might look then Castor is still able to help you with another tool.    

Figure 1. Castor conversion tools

By the way: a superior tool for making these kinds of conversions, and several others, is XMLSpy, but it has a price attached to it. 

For the following examples I'll use a typical web application deployment descriptor. I've taken the web.xml file from Jakarta Struts' "blank application", which is part of the Struts download. It has this format:

Listing 1. web.xml for the Struts-blank application
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- The Usual Welcome File List -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- Struts Tag Library Descriptors -->
  <taglib>
    <taglib-uri>/tags/struts-bean</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>

  . . . (several other taglibs follow) . . .
</web-app>

You'll see that the corresponding DTD is named at the top of the file, and you can find it in the struts.jar file if you're interested. It's not very well suited for our example, since the DTD is very large, and therefore will generate many, many classes. 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.