Reading XML as bean collection using Betwixt

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading XML as bean collection using Betwixt

atharva

How do I convert an XML to bean collection?

XML:
<persons>
    <person>
        <name>A</name>
        <age>21</age>
    </person>
    <person>
        <name>B</name>
        <age>25</age>
    </person>
    <person>
        <name>C</name>
        <age>32</age>
    </person>
</persons>

Java code:

beanReader.registerBeanClass("persons",  ArrayList.class);
beanReader.registerBeanClass("persons/person",  Person.class);
ArrayList persons = (ArrayList)beanReader.parse(xmlReader);

I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7

Thanks
- Ravi.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Reading XML as bean collection using Betwixt

eckobar
Does your collection-bean have adder-functions for the person-beans?

please see example at
http://commons.apache.org/betwixt/guide/examples.html (bottom of page).

Ravindra Wankar wrote:

>
> How do I convert an XML to bean collection?
>
> XML:
> <persons>
>    <person>
>        <name>A</name>
>        <age>21</age>
>    </person>
>    <person>
>        <name>B</name>
>        <age>25</age>
>    </person>
>    <person>
>        <name>C</name>
>        <age>32</age>
>    </person>
> </persons>
>
> Java code:
>
> beanReader.registerBeanClass("persons",  ArrayList.class);
> beanReader.registerBeanClass("persons/person",  Person.class);
> ArrayList persons = (ArrayList)beanReader.parse(xmlReader);
>
> I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7
>
> Thanks
> - Ravi.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Reading XML as bean collection using Betwixt

atharva

No. I'm trying to get all the person objects into an ArrayList.

Thanks
Ravi.

Eckenfellner Klaus wrote:
Does your collection-bean have adder-functions for the person-beans?

please see example at
http://commons.apache.org/betwixt/guide/examples.html (bottom of page).

Ravindra Wankar wrote:
  
How do I convert an XML to bean collection?

XML:
<persons>
   <person>
       <name>A</name>
       <age>21</age>
   </person>
   <person>
       <name>B</name>
       <age>25</age>
   </person>
   <person>
       <name>C</name>
       <age>32</age>
   </person>
</persons>

Java code:

beanReader.registerBeanClass("persons",  ArrayList.class);
beanReader.registerBeanClass("persons/person",  Person.class);
ArrayList persons = (ArrayList)beanReader.parse(xmlReader);

I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7

Thanks
- Ravi.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


    


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

  

--------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Reading XML as bean collection using Betwixt

Christian Grobmeier
Sorry if you get this twice, my email seems not  to get through to the list,
so i sent it again.



Hi Ravi,
i had the same problems and figured out that i need those .betwixt files.
Imagine a configuration having different entrys.

<?xml version="1.0" encoding="UTF-8"?>
<info primitiveTypes="attribute">
       <element name="configuration" class=''de.grobmeier.Configuration'>
               <element name="entry"
                                property="entry"
                                class='de.grobmeier.Entry'>
                       <addDefaults/>
               </element>
               <addDefaults/>
       </element>
</info>

Here is my Configuration class:

public class Configuration {
   private List<Entry> entries= new ArrayList<Entry>();
public List<Entry> getEntry() {
       return entries;
   }

   public void addEntry(_entry) {
       entries.add(_entry);
   }
}

Mind that addEntry Method please.
My Entry-Class is a simple POJO. I figured out i need a betwixt file
here to, altough i thought this was mapped "by default", however, its
similar to this:

<info primitiveTypes="attribute">
       <element
               name="entry"
               property="entry"
               class='de.grobmeier.Entry'>
               <attribute name='key' property='key'/>
               <attribute name='value' property='value'/>

      </element>
</info>

I had to register only the configuration class:
beanReader.registerBeanClass("configuration", Configuration.class);
           configuration = (Configuration)beanReader.parse(reader);

Hope this helps,
Best,
Chris

On 10/16/07, Ravindra Wankar <[hidden email]> wrote:

>
>
>  No. I'm trying to get all the person objects into an ArrayList.
>
>  Thanks
>  Ravi.
>
>
>  Eckenfellner Klaus wrote:
>  Does your collection-bean have adder-functions for the person-beans?
>
> please see example at
> http://commons.apache.org/betwixt/guide/examples.html
> (bottom of page).
>
> Ravindra Wankar wrote:
>
>
>  How do I convert an XML to bean collection?
>
> XML:
> <persons>
>  <person>
>  <name>A</name>
>  <age>21</age>
>  </person>
>  <person>
>  <name>B</name>
>  <age>25</age>
>  </person>
>  <person>
>  <name>C</name>
>  <age>32</age>
>  </person>
> </persons>
>
> Java code:
>
> beanReader.registerBeanClass("persons", ArrayList.class);
> beanReader.registerBeanClass("persons/person", Person.class);
> ArrayList persons = (ArrayList)beanReader.parse(xmlReader);
>
> I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7
>
> Thanks
> - Ravi.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Reading XML as bean collection using Betwixt

atharva
Hi Chris,

Thanks for the info. However, I think my case is slightly different. What I'm trying to do is not have to write the Configuration class that has the addEntry() and getEntry() methods and instead use an ArrayList directly. ie I'd like Betwixt to create an ArrayList and populate it with my POJOs using the add() method of the list.

-- Ravi

Christian Grobmeier wrote:
Sorry if you get this twice, my email seems not  to get through to the list,
so i sent it again.



Hi Ravi,
i had the same problems and figured out that i need those .betwixt files.
Imagine a configuration having different entrys.

<?xml version="1.0" encoding="UTF-8"?>
<info primitiveTypes="attribute">
       <element name="configuration" class=''de.grobmeier.Configuration'>
               <element name="entry"
                                property="entry"
                                class='de.grobmeier.Entry'>
                       <addDefaults/>
               </element>
               <addDefaults/>
       </element>
</info>

Here is my Configuration class:

public class Configuration {
   private List<Entry> entries= new ArrayList<Entry>();
public List<Entry> getEntry() {
       return entries;
   }

   public void addEntry(_entry) {
       entries.add(_entry);
   }
}

Mind that addEntry Method please.
My Entry-Class is a simple POJO. I figured out i need a betwixt file
here to, altough i thought this was mapped "by default", however, its
similar to this:

<info primitiveTypes="attribute">
       <element
               name="entry"
               property="entry"
               class='de.grobmeier.Entry'>
               <attribute name='key' property='key'/>
               <attribute name='value' property='value'/>

      </element>
</info>

I had to register only the configuration class:
beanReader.registerBeanClass("configuration", Configuration.class);
           configuration = (Configuration)beanReader.parse(reader);

Hope this helps,
Best,
Chris

On 10/16/07, Ravindra Wankar [hidden email] wrote:
  
 No. I'm trying to get all the person objects into an ArrayList.

 Thanks
 Ravi.


 Eckenfellner Klaus wrote:
 Does your collection-bean have adder-functions for the person-beans?

please see example at
http://commons.apache.org/betwixt/guide/examples.html
(bottom of page).

Ravindra Wankar wrote:


 How do I convert an XML to bean collection?

XML:
<persons>
 <person>
 <name>A</name>
 <age>21</age>
 </person>
 <person>
 <name>B</name>
 <age>25</age>
 </person>
 <person>
 <name>C</name>
 <age>32</age>
 </person>
</persons>

Java code:

beanReader.registerBeanClass("persons", ArrayList.class);
beanReader.registerBeanClass("persons/person", Person.class);
ArrayList persons = (ArrayList)beanReader.parse(xmlReader);

I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7

Thanks
- Ravi.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
    

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

  

--------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Reading XML as bean collection using Betwixt

Sergio Cuenca
In reply to this post by atharva
Ravindra Wankar <rwankar <at> celoxis.com> writes:

>
>
> How do I convert an XML to bean collection?
>
> XML:
> <persons>
>     <person>
>         <name>A</name>
>         <age>21</age>
>     </person>
>     <person>
>         <name>B</name>
>         <age>25</age>
>     </person>
>     <person>
>         <name>C</name>
>         <age>32</age>
>     </person>
> </persons>
>
> Java code:
>
> beanReader.registerBeanClass("persons",  ArrayList.class);
> beanReader.registerBeanClass("persons/person",  Person.class);
> ArrayList persons = (ArrayList)beanReader.parse(xmlReader);
>
> I get an empty list. I'm using Betwixt 0.8, Digester 1.8, BeanUtils 1.7
>
> Thanks
> - Ravi.
>


Hi,

In my case I must define the adder method in the betwixt file, like this:

<?xml version="1.0" encoding="UTF-8" ?>
<info primitiveTypes="element">
    <element name='record-bean'>
                <element name='name' property='name' />    
           
                <element name='changes'>
                        <element name='change' property='changes'
updater='addChanges'>
                                <element name='change_detail'
class='com.logister.sample.Change'/>
                        </element>
      </element>
    </element>
</info>

Hope this helps.
Regards.




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]