BeanIO - Segment writing problem

907 views
Skip to first unread message

Andrea Rastelli

unread,
Nov 16, 2012, 8:21:24 AM11/16/12
to bea...@googlegroups.com
Hi,

I have a problem with the BeanIO reader and writer when I use segment in my XML mapping file.

This is the xml mapping:
<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
<stream name="fileTabellaSconti" format="csv">
<parser>
<property name="delimiter" value=";" />
<property name="unquotedQuotesAllowed" value="true" />
<property name="whitespaceAllowed" value="true" />
</parser>
<record name="tabellaSconti" class="map" occurs="1">

<segment name="sconto" class="map" >
<field name="categoria" />
<field name="nome" />
<field name="tipologia" />
<field name="profilo" />
</segment>

<field name="valore_sconto" type="java.lang.Integer" />
<field name="codice_sts" />
<field name="sconto_equivalente" type="java.lang.Integer" />

</record>
</stream>
</beanio>

And this is the writer:
public static void writer()
{
// create a StreamFactory
StreamFactory factory = StreamFactory.newInstance();
// load the mapping file
factory.load(new File("map_sconto.xml"));


BeanWriter out = factory.createWriter("fileTabellaSconti", new File("employee.csv"));
int c = 0;
while (c < 5)
{
c++;
Map<String, Object> record = new HashMap<String, Object>();


Map<String, Object> sconto = new HashMap<String, Object>();


sconto.put("categoria", "cat_"+c);
sconto.put("nome", "nome_"+c);
sconto.put("tipologia", "tipologia_"+c);
sconto.put("profilo", "profilo_"+c);


record.put( "sconto" , sconto );


record.put( "valore_sconto" , c );
record.put( "codice_sts" , "sts_"+c );
record.put( "sconto_equivalente" , c);
System.out.println(record);
out.write(record);
}
out.flush();
out.close();
}
Why the CSV generated contains this rows?
1,sts_1,1
2,sts_2,2
3,sts_3,3
4,sts_4,4
5,sts_5,5
 

Kevin

unread,
Nov 16, 2012, 11:49:26 PM11/16/12
to bea...@googlegroups.com
Hi Andrea,

I changed the record occurs to 5 and ran your code using the latest 2.x code and got the following result:

cat_1;nome_1;tipologia_1;profilo_1;1;sts_1;1
cat_2;nome_2;tipologia_2;profilo_2;2;sts_2;2
cat_3;nome_3;tipologia_3;profilo_3;3;sts_3;3
cat_4;nome_4;tipologia_4;profilo_4;4;sts_4;4
cat_5;nome_5;tipologia_5;profilo_5;5;sts_5;5

This is what I would expect from that mapping file.  What did you expect instead?

Thanks,
Kevin

Andrea Rastelli

unread,
Nov 19, 2012, 4:46:09 AM11/19/12
to bea...@googlegroups.com
First of all I have this error:
Bean identification failed: no record or group mapping for bean class 'class java.util.HashMap' at the current position

Even specifying:
<record name="tabellaSconti" class="java.util.HashMap" occurs="1">

Kevin

unread,
Nov 19, 2012, 2:08:04 PM11/19/12
to bea...@googlegroups.com
Hi Andrea,

Like I said, you have to set occurs to "5", or "1+", and not "1".  Otherwise you will only be able to write/marshal a single HashMap.

Thanks,
Kevin
Reply all
Reply to author
Forward
0 new messages