You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jason Wood <ja...@auto-trol.com> on 2005/01/17 16:39:20 UTC

[betwixt] collections and writing XML

I am trying to write the following bean (OutterClass.java) to XML, but the
resulting XML is not what I want.

// base class for the two beans add to the collection
public interface BaseClass
{
}

// will be added to the collection
public class DerivedClassA implements BaseClass
{
}

public class DerivedClassB implements BaseClass
{
}

// the class to be converted to XML.
public class OutterClass
{
  Vector attributes = new Vector();

  public OutterClass()
  {
    attributes.add(new DerivedClassA());
    attributes.add(new DerivedClassB());
  }

  public  Vector getAttributes()
  {
    return attributes;
  }
}

// .betwixt file
<?xml version='1.0' encoding='UTF-8' ?>
<info>
<element name='MyOutterClass'>
 <addDefaults/>
</element>
</info>

// XML written.
<MyOutterClass>
  <MyDerivedClassA/>
  <DerivedClassB/>
</MyOutterClass>

The XML above is perfect, but when I add the following method below to
OutterClass...

 public void addAttribute(BaseClass attr)
 {
   attributes.add(attr);
 }


the XML output changes to...

<MyOutterClass>
  <attribute/>
  <attribute/>
</MyOutterClass>

----------------------------------------------------------------------------
---------------------------
I have also tried the following .betwixt file...

<?xml version='1.0' encoding='UTF-8' ?>
<info>
<element name='MyOutterClass'>
 <addDefaults add-adders='false'/>
</element>
</info>

but the XML output changes to...

<MyOutterClass/>



Any idea why this is happening and how to fix it?

Thanks
J


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [betwixt] collections and writing XML

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
the reason for this change is that once the adder is added, betwixt is  
able to guess the property name and (by default) starts using that for  
the contained elements.

you have just entered the mysterious area of derived beans. mysterious  
for two reasons: first, it's a subtle and complex subject; second,  
because it's been a while and i've forgotten what progress had been  
made...

there is something on the website  
http://jakarta.apache.org/commons/betwixt/guide/derived.html and i'd  
suggest that you start with that and the source from CVS HEAD since i'm  
still catching up on mail today. you might find that the required  
functionality has already been added, otherwise i'll try to get back  
later this week...

- robert

On 17 Jan 2005, at 15:39, Jason Wood wrote:

> I am trying to write the following bean (OutterClass.java) to XML, but  
> the
> resulting XML is not what I want.
>
> // base class for the two beans add to the collection
> public interface BaseClass
> {
> }
>
> // will be added to the collection
> public class DerivedClassA implements BaseClass
> {
> }
>
> public class DerivedClassB implements BaseClass
> {
> }
>
> // the class to be converted to XML.
> public class OutterClass
> {
>   Vector attributes = new Vector();
>
>   public OutterClass()
>   {
>     attributes.add(new DerivedClassA());
>     attributes.add(new DerivedClassB());
>   }
>
>   public  Vector getAttributes()
>   {
>     return attributes;
>   }
> }
>
> // .betwixt file
> <?xml version='1.0' encoding='UTF-8' ?>
> <info>
> <element name='MyOutterClass'>
>  <addDefaults/>
> </element>
> </info>
>
> // XML written.
> <MyOutterClass>
>   <MyDerivedClassA/>
>   <DerivedClassB/>
> </MyOutterClass>
>
> The XML above is perfect, but when I add the following method below to
> OutterClass...
>
>  public void addAttribute(BaseClass attr)
>  {
>    attributes.add(attr);
>  }
>
>
> the XML output changes to...
>
> <MyOutterClass>
>   <attribute/>
>   <attribute/>
> </MyOutterClass>
>
> ----------------------------------------------------------------------- 
> -----
> ---------------------------
> I have also tried the following .betwixt file...
>
> <?xml version='1.0' encoding='UTF-8' ?>
> <info>
> <element name='MyOutterClass'>
>  <addDefaults add-adders='false'/>
> </element>
> </info>
>
> but the XML output changes to...
>
> <MyOutterClass/>
>
>
>
> Any idea why this is happening and how to fix it?
>
> Thanks
> J
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org