Solved

Castor XML API unmarshal to inherited classes

Posted on 2004-03-28
4
731 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
return in catch statement 1 40
endX challenge 2 50
word0 challenge 4 54
get weblogic logged in user in java 2 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now