Solved

Castor XML API unmarshal to inherited classes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy:unable to resolve class error 2 65
diffSum example 4 19
MySQL  on Tomcat 8 22
expectj telnet failing 5 17
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

943 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

1 Experts available now in Live!

Get 1:1 Help Now