commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Holger Düwiger <duewi...@neofonie.de>
Subject [betwixt] Help with collection/.betwixt
Date Wed, 07 Jul 2004 08:25:42 GMT
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message