Stephen Bash wrote:
Steven-

You've got the right ideas, there's just something wrong with the
implementation (which we'll try to figure out).  First question is
what version of Castor are you using?  Next, what method are you
calling to unmarshal the XML?  If you use one of the static unmarshal
calls, then the mapping you have loaded won't take affect.

To address some of your specific issues.  I agree CustomerWrapper
should be mapped using the location attribute.  Why it doesn't work is
something else.

It's not working because the location="CustomerWrapper" needs to be on the <bind-xml> element and not the <field> element.

--Keith


The WrapperDetail is really an extra element, so you should look into
Unmarshaller.setIgnoreExtraElements, which instructs an unmarshaller
to ignore elements that don't exist in the mapping file.

Finally, there are two ways to handle the Items element.  You can
either use the location attribute similar to CustomerWrapper, or you
can use the container attribute (goes in the field element in the
mapping file).  I'm assuming your Java class has an array or List of
Item objects that turn into the individual items in the XML.  If you
set container="false" (and provide the correct bind-xml name="Items")
Castor will create the Items element for you.

HTH,
Stephen

On 3/29/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

Hi,

    I've started using Castor and while the docs are pretty good,
they're a bit short on complex mapping examples.  I can't seem to find
anywhere elsewhere, though I'm still digging through the list archive
for castor-users (sadly, searching the list archive for "complex
mapping" didn't turn up much).

    I'm using Castor for java-to-XML mapping where I have an
existing, mostly-defined XML and Java classes to deal with.  Here's an
example of the sort of mapping I'm having problems with right now:

-----Order Example with <Items></Items> tags
<Order>
   <CustomerWrapper>
       <WrapperDetail>uselessValue</WrapperDetail>
       <Customer>
           <Recipient>Bob</Recipient>
           <Address>123 Public Street</Address>
           <ZipCode>11111111</ZipCode>
       </Customer>
   </CustomerWrapper>
   <Items>
       <Item>
           <Sku>value</Sku>
           <Description>value</Description>
           <Manufacturer>value</Manufacturer>
       </Item>
       <Item>
           <Sku>value</Sku>
           <Description>value</Description>
           <Manufacturer>value</Manufacturer>
       </Item>
       <Item>
           <Sku>value</Sku>
           <Description>value</Description>
           <Manufacturer>value</Manufacturer>
       </Item>
       <!-- ...repeat Item tags ad nauseam... -->
   </Items>
</Order>
-----

The problems I'm having are with the
<CustomerWrapper></CustomerWrapper> surrounding the <Customer>, the
<WrapperDetail>, neither of which map to a java class (I'd like to
just ignore them) and the <Items></Items> surrounding the multiple
<Item> elements.  When I drop those out of the data, everything works
fine with the following mapping files.

According to the docs, I should be able ignore <CustomerWrapper> by
specifying the fields like:

       <field name="recipient" type="String" location="CustomerWrapper"
node="text">
           <bind-xml name="Recipient"/>
       </field>

But when I do so and add the <CustomerWrapper> back in, I get:

Unable to find FieldDescriptor for 'CustomerWrapper' in ClassDescriptor of
sol-and-offers{file: [not available]; line: 2; column: 29}

Similarly, when I add the <Items></Items> back in (without the other
changes, I get:

"unable to find FieldDescriptor for 'Items' in ClassDescriptor of
Order{file: [not available]; line: 51; column: 10}"


-----Customer Mapping
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
   "http://castor.org/mapping.dtd";>
<mapping>
   <class name="com.foo.Customer">
       <map-to xml="Customer"/>
       <field name="recipient" type="String" node="text">
           <bind-xml name="Recipient"/>
       </field>
       <field name="address" type="String" node="text">
           <bind-xml name="Address"/>
       </field>
       <field name="zipCode" type="String" node="text">
           <bind-xml name="ZipCode"/>
       </field>
   </class>
</mapping>
-----

-----Item Mapping
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
   "http://castor.org/mapping.dtd";>
<mapping>
   <class name="com.foo.Item">
       <field name="sku" type="String" node="text">
           <bind-xml name="Sku"/>
       </field>
       <field name="description" type="String" node="text">
           <bind-xml name="Description"/>
       </field>
       <field name="manufacturer" type="String" node="text">
           <bind-xml name="Manufacturer"/>
       </field>
   </class>
</mapping>
-----

-----Order Mapping
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
   "http://castor.org/mapping.dtd";>
<mapping>
   <include href="CustomerMapping.xml"/>
   <include href="ItemMapping.xml"/>
   <class name="com.foo.Order">
       <field name="customer" type="com.foo.Customer">
           <xml bind-to="Customer"/>
       </field>
       <field name="Items" type="com.foo.Item" collection="arraylist"/>
   </class>
</mapping>
-----

Steven J. Owens
[EMAIL PROTECTED]



-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------







-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to