Resolved...

I had left out the " primitiveTypes='element'" on the child object .betwixt.

Thanks anyway.






[EMAIL PROTECTED]
12/29/2004 10:54 AM
Please respond to "Jakarta Commons Users List"

 
        To:     [EMAIL PROTECTED]
        cc:     [email protected]
        Subject:        Re: [betwixt] Help with collection/.betwixt


Robert,

I am trying to do almost exactly what Holger is below, and I'm running 
into a problem reading a bean from XML.  The problem is around reading a 
collection of "Asset" elements.  Here is my scenario:

Example XML:

<Envelope>
   <Body>
     <Response>
         <Contract pk="500-0847281-000">
            <ContCustomerCreditAcct>33230373</ContCustomerCreditAcct>
            <Asset pk="153384">
               <AsEquipDesc>COPIER</AsEquipDesc>
            </Asset>
            <Asset pk="153385">
               <AsEquipDesc>COPIER</AsEquipDesc>
               <!-- <equipDesc>COPIER</equipDesc>  // this would work -->
            </Asset>
         </Contract>
      </Response>
   </Body>
</Envelope>


Java class:


public class AgmtDetailVO extends ValueObject 
{
 
   /**
    * This field contains the list of assets.
    */

   public java.util.Collection  assets = new ArrayList();
 
   public void addAsset(AssetVO asset) {
        assets.add(asset);
   }

   public java.util.Collection  getAssets() {
        return assets;
   }

 
   /**
    * This field contains agreement number.
    */
   public String agmtNum = null;
/**
 * @return
 */
public String getAgmtNum() {
        return agmtNum;
}
/**
 * @param string
 */
public void setAgmtNum(String string) {
        agmtNum = string;
} 
   /**
    * This field contains the customer name.
    */
   public String custName = null;}
/**
 * @param string
 */
public void setCustName(String string) {
        custName = string;
}
/**
 * @return
 */
public String getCustName() {
        return custName;
}
}


Example .betwixt:

<?xml version="1.0" encoding="UTF-8"?> 
<info primitiveTypes='element'>
<element name='Contract' >
        <attribute name='pk' property='agmtNum'/>
        <element name='ContCustomerName' property='custName'/>
        <element name='ContCalcPymtSchedule' property='pymtFreq'/>
        <element name='ContOriginalTerm' property='contractTerm'/>
        <element name='Asset' property='assets' >
                <attribute name='pk' property='assetNumber'/>
                <element name='AsEquipDesc' property='equipDesc'/>
                <element name='AsModel' property='modelNum'/>
                <element name='AsSerialNumber' property='serialNum'/>
                <element name='LocEquipZip' property='equipZip'/>
                <!--<addDefaults/>-->
        </element>
        <addDefaults/>
</element>
</info>

When the Asset elements are read to build the assets collection, I do see 
AssetVO objects being instantiated and added to the list.  The issue I'm 
running into is that the properties of these AssetVO objects are not being 

set according to the .betwixt file; they are empty.  I have found no 
difference in behavior if I include the addDefaults element or not.  What 
I have found, is that if I add a default-named element, for example 
<equipDesc>, to the XML file, then the property *is* set.  This makes it 
appear that the .betwixt mappings for the collection item object are being 

ignored.  The top-level properties for the AgmtDetailVO object are all 
being set correctly, according to their .betwixt mappings.

Is there something that I'm missing here as far as syntax, or is there a 
limitation with betwixt in reading these nested objects?

Thanks in advance for any help.

Don

-------------------------------------------------------------



Re: [betwixt] Help with collection/.betwixt
robert burrell donkin
Thu, 08 Jul 2004 14:03:10 -0700
hi Holger

i think that you have one small mistake in your betwixt file. the property 

attribute value needs to be the name of the getter rather than the adder 
and so 'inners' rather than 'inner'.

the following works for me (with CVS HEAD).

<?xml version="1.0"?>
<info primitiveTypes="element">
 <element name="results">
   <element name="row" property="inners"/>
   <addDefaults/>
 </element>
</info>

- robert

On 7 Jul 2004, at 09:25, Holger D�wiger wrote:

Hi,

I'm trying to use betwixt to populate my bean from a given
XML source. The code below shows what I'm trying to
do, giving me an empty "ExampleBean".
When I change the XML-tag "row" to "inner" (in the .betwixt
resective.), everything works as I expect.

Anyway, in my real world problem the XML format is given
and I can't change.

Am I missing something?

Thanks,
Holger
-------------------------- Test.java ----------------------------
import java.io.StringReader;

import org.apache.commons.betwixt.io.BeanReader;

public class Test{

 public static void main(String argv[]) throws Exception {
   ExampleBean bean = new ExampleBean();

   String xml = "<?xml version='1.0'?>" +
     "<results>" +
     "<row><id>1</id><name>A</name></row>" +
     "<row><id>2</id><name>B</name></row>" +
     "<row><id>3</id><name>C</name></row>" +
     "</results>";
      /* This works well
   String xml = "<?xml version='1.0'?>" +
     "<results>" +
     "<inner><id>1</id><name>A</name></inner>" +
     "<inner><id>2</id><name>B</name></inner>" +
     "<inner><id>3</id><name>C</name></inner>" +
     "</results>";
   */

BeanReader reader = new BeanReader();
reader.getXMLIntrospector().setWrapCollectionsInElement(false);
reader.registerBeanClass("results", ExampleBean.class);
StringReader in = new StringReader(xml);
ExampleBean out = (ExampleBean)reader.parse(in);
System.out.println(out);
}
}
-------------------------- ExampleBean.java ----------------------------
import java.util.ArrayList;
import java.util.Collection;

public class ExampleBean{

   private Collection inners = new ArrayList();
      public ExampleBean(){};

   public Collection getInners(){
       return this.inners;
   }
   public void addInner(InnerBean i){
       inners.add(i);
   }

public String toString(){
return "[" + this.getClass().getName() + ": inners = " + inners + "]";
}

}
--------------------------- InnerBean.java 
--------------------------------------------
public class InnerBean{
private String id;
private String name;

   public InnerBean(){}
   public void setId(String s){
       this.id = s;
   }
   public void setName(String s){
       this.name = s;
   }

   public String getId(){
       return id;
   }
   public String getName(){
       return name;
   }

public String toString(){
return "[" + this.getClass().getName() + ": name=" + name + ", id=" + id + 

"]";
}

}
--------------------------- ExampleBean.betwixt 
---------------------------------------------
<?xml version="1.0"?>
<info primitiveTypes="element">
<element name="results">
<element name="row" property="inner"/>
<!-- element name="inner" property="inner"/-->
<addDefaults/>
</element>
</info>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


------------------------------------------------------------------------------
Electronic Privacy Notice. This e-mail, and any attachments, contains 
information that is, or may be, covered by electronic communications 
privacy laws, and is also confidential and proprietary in nature. If you 
are not the intended recipient, please be advised that you are legally 
prohibited from retaining, using, copying, distributing, or otherwise 
disclosing this information in any manner. Instead, please reply to the 
sender that you have received this communication in error, and then 
immediately delete it. Thank you in advance for your cooperation.
==============================================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


------------------------------------------------------------------------------
Electronic Privacy Notice. This e-mail, and any attachments, contains 
information that is, or may be, covered by electronic communications 
privacy laws, and is also confidential and proprietary in nature. If you 
are not the intended recipient, please be advised that you are legally 
prohibited from retaining, using, copying, distributing, or otherwise 
disclosing this information in any manner. Instead, please reply to the 
sender that you have received this communication in error, and then 
immediately delete it. Thank you in advance for your cooperation.
==============================================================================





------------------------------------------------------------------------------
Electronic Privacy Notice. This e-mail, and any attachments, contains 
information that is, or may be, covered by electronic communications privacy 
laws, and is also confidential and proprietary in nature. If you are not the 
intended recipient, please be advised that you are legally prohibited from 
retaining, using, copying, distributing, or otherwise disclosing this 
information in any manner. Instead, please reply to the sender that you have 
received this communication in error, and then immediately delete it. Thank you 
in advance for your cooperation.
==============================================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to