Solved

Castor XML API unmarshal to inherited classes

Posted on 2004-03-28
4
771 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

628 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