1

This is my problem: I must use BeanIO to read a CSV. This CSV is something like:

s1_el1;s1_el2;s1_el3;s1_el4;X1;Y1;Z1
s2_el1;s2_el2;s2_el3;s2_el4;X2;Y2;Z2
s2_el1;s2_el2;s2_el3;s2_el4;X3;Y3;Z3

Where the sN_elM (where N and M are incremental values for row and column) must be placed in a section (BeanIO section).

What I actually have is a mapping XML like this:

<?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">           
            <segment name="sconto" class="map" >
                <field name="categoria" />
                <field name="nome" />
                <field name="tipologia" />
                <field name="profilo" />
            </segment>          
            <field name="valoreSconto" type="java.lang.Integer" />
            <field name="codiceSts" />
            <field name="scontoEquivalente" type="java.lang.Integer" />         
        </record>       
    </stream>   
</beanio>

In my "writer()" function I do this:

public static void writer( File csv_file )
{
    factory.load(new File(user_dir+"/docroot/WEB-INF/src/it/saleshub/csv/mapping/map_sconto.xml"));

    BeanWriter out = factory.createWriter( "fileTabellaSconti", csv_file );

    int c = 0;
    while (c < 5)
    {
        c++;

        HashMap<String, Object> record = new HashMap<String, Object>();

        HashMap<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( "valoreSconto",     new Integer(c) );
        record.put( "codiceSts",        "sts_"+c );
        record.put( "scontoEquivalente",new Integer(c) );

        System.out.println(record);
        out.write(record);

    }

    out.flush();
    out.close();
}

But every time I use this function, the code show me this exception:

Bean identification failed: no record or group mapping for bean class 'class java.util.HashMap' [...]

Where is my error? I think I'm using in a wrong way the segment, but I can't find any kind of documentation about how to use it correctly..

4

1 回答 1

1

问题在于您的映射文件和您的 pojo 类。

对于映射文件中定义的每个字段,都应该在父标记中定义的类中具有 setter 和 getter 方法,无论是段还是记录。

在这里,您使用了一个类映射,我认为它在您的代码中不能用作 pojo。此外,如果您想在段上使用集合,您需要使用集合属性并提供值作为地图或列表。

    <record name="tabellaSconti" class="com.test.Parent">           
        <segment name="scontos" collection="list" class="com.test.Sconto" >
            <field name="categoria" />
            <field name="nome" />
            <field name="tipologia" />
            <field name="profilo" />
        </segment>
        <field name="valoreSconto" type="java.lang.Integer" />
        <field name="codiceSts" />
        <field name="scontoEquivalente" type="java.lang.Integer" />         
    </record> 

在 com.test.Parent 中,您必须定义 getScontos 和 setScontos 方法以及用于侧段之外的字段的其他 getter 和 setter 方法,同样在 Sconto 类中,您必须为段中定义的所有字段定义 getter 和 setter。

最后你的主类中的代码是

    Parent record = new Parent;
    Sconto sconto = new Sconto();
    List<Sconto> scontos = new ArrayList<Sconto>();

    sconto.setCategoria("cat_"+c );
    sconto.setNome("nome_"+c );
    sconto.setTipologia("tipologia_"+c );
    sconto.setProfilo("profilo_"+c );

    scontos.add(sconto);

    record.setScontos(scontos);

    -------
    ---------
    out.write(record);
于 2013-03-02T18:46:24.883 回答