【发布时间】:2012-11-19 10:16:12
【问题描述】:
这是我的问题:我必须使用 BeanIO 来读取 CSV。 这个 CSV 类似于:
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
其中 sN_elM(其中 N 和 M 是行和列的增量值)必须放在一个节(BeanIO 节)中。
我实际上拥有的是这样的映射 XML:
<?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>
在我的“writer()”函数中,我这样做:
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();
}
但是每次我使用这个函数时,代码都会显示这个异常:
Bean identification failed: no record or group mapping for bean class 'class java.util.HashMap' [...]
我的错误在哪里? 我认为我以错误的方式使用了该细分市场,但我找不到任何关于如何正确使用它的文档..
【问题讨论】:
-
我用 BeanIO 2.0.2 测试了你的代码,它运行得很好。 csv 中有五行,如下所示:
cat_1;nome_1;tipologia_1;profilo_1;1;sts_1;1 -
我不明白为什么我有这个异常..我使用最新的 BeanIO jar(2.0.2 像你的)
标签: java exception csv bean-io