commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Help mapping an ArrayList derived object
Date Mon, 07 Apr 2008 23:06:13 GMT

Okay - so my disclaimer is that I don't claim to be overly knowledgeable about
Betwixt. I joined the list to ask some questions about Betwixt and no one
answered me, so I've tried to help anyone that has questions.

Given your input - I'd say that LayerNode "has-a" (array) list, not that
LayerNode "is-a" (array) list.  Because LayerNode has attributes (chiefly
'name', and maybe other elements) that make it more than just a subclass of
ArrayList<FieldNode>. (The Head Start Design Patterns book has an example, a
"Bath is-a Tub" doesn't sound right - but a "Bath has-a Tub" does sound right.
I may be wrong here in the view on the containment relation, but I think it
works better)

Now, there is a way to tell the BeanReader that your XML has a collection that
is not wrapped in a "containing" element.  Which is your case here.  But I'll
get to that after explaining how I've mapped the objects.

This is the approach that I'd try:

<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='element'>
   <element name='LayerNode'>
     <attribute name="name" property="name"/>
     <element name="FieldNode" property="nodes" updater="addFieldNode"/>
public class LayerNode {
   private List<FieldNode> nodes = new ArrayList<FieldNode>();
   private String name;

   public void addFieldNode(FieldNode fnode) {

/* important - you need to have your getters/setters, I've omitted them 
- but I
defined them in accordance with what you'd expect for a bean/pojo */

(note - I added 'id' when I was trying to map this without your sample...)

<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='element'>
   <hide property='position'/>
   <element name='FieldNode'>
     <attribute name="id" property="id"/>
     <attribute name="name" property="name"/>
public class FieldNode {
	private Long id;
	private String name;
	private int position;
/* bean/pojo getters/setters omitted */

I wrote a little JUnit test to see if it was loading correctly:

public void testDocumentLoad() { // class omitted to save space
   try {
     LayerNode root = getRootNode("./text.xml");
   } catch (Exception e) {
     e.printStackTrace(); System.out.println("exception occured... lame");

public LayerNode getRootNode(String filename) {
   try { // this assumes 'filename' is on the classpath
     InputStream xtnodes = getClass().getResourceAsStream(filename);
     BeanReader beanReader = new BeanReader();
     return (LayerNode)beanReader.parse(xtnodes);
   } catch (Exception e) {
     e.printStackTrace(); System.out.println("exception occured... lame");
     return null;

file: text.xml (file used)
<?xml version='1.0' encoding='UTF-8' ?>
<LayerNode name="a">
    <FieldNode id="650" name="a1"/>
    <FieldNode id="999" name="a2"/>

I was able to get LayerNode loaded with the two FieldNode instances, 
with their
id and name properties populated.

I've had similar issues in my projects.  I found odd things like, the 'name'
attribute of an element wouldn't get automatically mapped to the 'name'
property of the corresponding object.  So what I'd do is get something working
- then slowly add to the xml and the object.  I still don't feel too
comfortable with Betwixt overall.  And it seemed nearly impossible to get help
- so I hope this helps you out.


  * @author:  Andrew Lenards
  * @email:
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website:
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message