You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Low Han Ming <lo...@yahoo.com.sg> on 2008/04/03 15:24:54 UTC

[Betwixt] Help : Writing collection of objects to sub element instead of attribute

Hi,
 
I'm new to Betwixt.
I would really appreciate if someone can advice on how to output a
collection of custom class of object into a sub element in the XML instead
of attribute.
 
I would like my output to be 

<?xml version='1.0' ?>
<person name="John Smith">
  <items>
    <Item><id>a</id></Item>
    <Item><id>b</id></Item>
  </items>
</person>

But instead, the output was 

<?xml version='1.0' ?>
<person name="John Smith">
  <items>
    <Item id="a"/>
    <Item id="b"/>
  </items>
</person>

The main difference is the "id" under <item> being an attribute instead of
an element.

I greatly appreciate if you can advise or point me to some sample that can
do the job.
 

Thanks.
 

Han Ming

 

 

Code
########################################################
PersonBean
public class PersonBean {
private String name;
ArrayList items;

public ArrayList getItems() {return items;}
public void setItems(ArrayList items) {this.items = items;}

public String getName() {return name;}
public void setName(String name) {this.name = name;} 

}

 
Item 
public class Item {
String id;

public String getId() {return id;}
public void setId(String id) {this.id = id;}
}

 
main class .....
PersonBean person = new PersonBean();
person.setName("John Smith");

ArrayList items = new ArrayList();

Item item1 = new Item();
item1.setId("a");

Item item2 = new Item();
item2.setId("b");

items.add(item1);
items.add(item2);

person.setItems(items);

 




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


RE: [Betwixt] Help : Writing collection of objects to sub elementinstead of attribute

Posted by Low Han Ming <lo...@yahoo.com.sg>.
Hi Andy,

That's solve my problem exactly. :)

Just for information for beginner like me. 

You can use this sample below.

Thanks again.

Cheers.


Han Ming




public class PersonBean {
    private String name;
    ArrayList items;
    ArrayList products;
    
    public ArrayList getProducts() {return products;}
    public void setProducts(ArrayList products) {this.products = products;}

    public ArrayList getItems() {return items;}
    public void setItems(ArrayList items) {this.items = items;}
}

public class Item {
    String id;

    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
}


PersonBean.betwixt
****************************************
<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes="attribute">
  <element name="person">
    <element name="name" property="name" />
    
    <element name="lotsofitems" >
       <element name="oneitem" property="items" class="betwixt.Item" />
    </element>
  </element>
</info>



Item.betwixt
****************************************
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
  	<element name="item" property="item" >
      <element name="id" property="id"/>
    </element>
</info>

-----Original Message-----
From: lenards@email.arizona.edu [mailto:lenards@email.arizona.edu] 
Sent: Friday, April 04, 2008 1:49 AM
To: user@commons.apache.org
Subject: Re: [Betwixt] Help : Writing collection of objects to sub
elementinstead of attribute

Hello Han Ming,

I'm also somewhat new to Betwixt, but have gotten a number of things to 
work so
I'll take a crack at your question.

What do does you betwixt-config or .betwixt file look like for the 
Person class?

I belive that if you're depending on betwixt to infer the output it 
will assume
that properties are to be treated as xml-attributes of a given xml-element.

try placing an Item.betwixt in the same directory with the Item.java

Item.betwixt
--------------------
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
   <element name="person">
     <element name="id" property="id"/>
     <addDefaults />
   </element>
</info>

I'd expect the output to be what you were expecting:

> <?xml version='1.0' ?>
> <person name="John Smith">
>   <items>
>     <Item><id>a</id></Item>
>     <Item><id>b</id></Item>
>   </items>
> </person>

Andy

--
/****************************************
  * @author: Andrew Lenards
  * @email:  lenards@email.arizona.edu
  * @title:  Systems Programmer, Principal
  * @dept:   Tree of Life Web Project,
  *          Dept. of Entomology
  * @url:    http://tolweb.org/
  * @school: University of Arizona
  * @geo-loc:Tucson, AZ 85721
  ***************************************/


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



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


Re: [Betwixt] Help : Writing collection of objects to sub element instead of attribute

Posted by le...@email.arizona.edu.
Hello Han Ming,

I'm also somewhat new to Betwixt, but have gotten a number of things to 
work so
I'll take a crack at your question.

What do does you betwixt-config or .betwixt file look like for the 
Person class?

I belive that if you're depending on betwixt to infer the output it 
will assume
that properties are to be treated as xml-attributes of a given xml-element.

try placing an Item.betwixt in the same directory with the Item.java

Item.betwixt
--------------------
<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
   <element name="person">
     <element name="id" property="id"/>
     <addDefaults />
   </element>
</info>

I'd expect the output to be what you were expecting:

> <?xml version='1.0' ?>
> <person name="John Smith">
>   <items>
>     <Item><id>a</id></Item>
>     <Item><id>b</id></Item>
>   </items>
> </person>

Andy

--
/****************************************
  * @author: Andrew Lenards
  * @email:  lenards@email.arizona.edu
  * @title:  Systems Programmer, Principal
  * @dept:   Tree of Life Web Project,
  *          Dept. of Entomology
  * @url:    http://tolweb.org/
  * @school: University of Arizona
  * @geo-loc:Tucson, AZ 85721
  ***************************************/


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