Solved

Castor XML API unmarshal to inherited classes

Posted on 2004-03-28
4
747 Views
Last Modified: 2008-02-01
I am trying to learn Castor XML and currently have the following situation.  I have three Java classes: Tank, SquareTank, & RoundTank.  SquareTank and RoundTank extend Tank.  I am trying to set up a Castor mapping file to read from an XML file and create a SquareTank object.  

Here are the two variables in the Tank class that are marshalled to the XML file and should be unmarshalled from the XML file:

public class Tank implements java.io.Serializable
{
  protected double WaterLevel; //Water level of the tank
  protected double TankHeight; //height of the tank
}

Here is the three variables in the SquareTank class that are marshalled to the XML file

public class SquareTank extends Tank implements java.io.Serializable
{
  protected double tankWidth; //width of the square tank
  protected double tankVolume; //volume of the tank
  protected double waterVolume; //volume of the water in the tank
}


Here are the three variables in the RoundTank class that are marshalled to the XML file and should be
unmarshalled from the XML file

public class RoundTank extends Tank implements java.io.Serializable
{
  protected double tankDiameter;
  protected double tankVolume;
  protected double waterVolume;
}

Here is a sample XML file that I am trying to unmarshal from:
<?xml version="1.0"?>
<WaterTanks xmlns="http://www.benchmark.com//squareTank"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.benchmark.com//squareTank WaterTanks.xsd">
      <SquareTank>
            <tankWidth>8.8</tankWidth>
            <tankVolume>867.33</tankVolume>
            <waterVolume>549.82</waterVolume>
            <TankHeight>11.2</TankHeight>
            <WaterLevel>7.1</WaterLevel>
      </SquareTank>
</WaterTanks>

And here is the mapping file that I currently have set up:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
                     "http://castor.exolab.org/mapping.dtd">

<mapping>
     <class name="watertanks.Tank" auto-complete="false">
      <description>Default mapping for class Tank</description>
      <map-to xml="watertanks.Tank"/>
      <field name="WaterLevel" type="double">
             <bind-xml name="WaterLevel" node="element"/>
      </field>
      <field name="TankHeight" type="double">
             <bind-xml name="TankHeight" node="element"/>
      </field>
     </class>
     <class name="watertanks.SquareTank" extends="watertanks.Tank" auto-complete="false">
      <description>Default mapping for class SquareTank</description>
      <map-to xml="SquareTank"/>
      
      <!--<field name="TankHeight" type="double">
              <bind-xml name="TankHeight" node="element"/>
      </field>
      <field name="WaterLevel" type="double">
                <bind-xml name="WaterLevel" node="element"/>
      </field>-->
      <field name="tankWidth" type="double">
              <bind-xml name="tankWidth" node="element"/>
      </field>
      <field name="tankVolume" type="double">
               <bind-xml name="tankVolume" node="element"/>
      </field>
      <field name="waterVolume" type="double">
               <bind-xml name="waterVolume" node="element"/>
      </field>
      <field name="Tank" type="watertanks.Tank"/>
     </class>
     <class name="watertanks.RoundTank" extends="watertanks.Tank" auto-complete="false">
      <description>Default mapping for class RoundTank</description>
      <map-to xml="RoundTank"/>
      
      <!--<field name="TankHeight" type="double">
              <bind-xml name="TankHeight" node="element"/>
      </field>
      <field name="WaterLevel" type="double">
                <bind-xml name="WaterLevel" node="element"/>
      </field>-->
      <field name="tankDiameter" type="double">
               <bind-xml name="tankDiameter" node="element"/>
      </field>
      <field name="tankVolume" type="double">
               <bind-xml name="tankVolume" node="element"/>
      </field>
      <field name="waterVolume" type="double">
               <bind-xml name="waterVolume" node="element"/>
      </field>
      <field name="Tank" type="watertanks.Tank"/>
      </class>
</mapping>

When I run the program to unmarshal, I get the following error:
org.xml.sax.SAXException: unable to find FieldDescriptor for 'watertanks.SquareTank' in ClassDescriptor of SquareTank

Any suggestions would be greatly appreciated.  Thanks.
0
Comment
Question by:bfinnso
  • 2
  • 2
4 Comments
 

Author Comment

by:bfinnso
ID: 10701367
Forgot to mention, Tank.java, SquareTank.java, & RoundTank.java are all in a package called watertanks.  Hence, the references to watertanks in the xml mapping file.  Thanks.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 350 total points
ID: 10702897
This is not an API i know, but this looks wrong:

>>


<WaterTanks xmlns="http://www.benchmark.com//squareTank"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.benchmark.com//squareTank WaterTanks.xsd">

>>

There are double slashes where there should be single ones and a missing one in the last (maybe the 'breaker')
0
 

Author Comment

by:bfinnso
ID: 10706820
I actually figured it out, so you just made an easy 350 points.  Congrats!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10706829
8-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question