Solved

Castor XML API unmarshal to inherited classes

Posted on 2004-03-28
4
752 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

733 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