1

给定以下课程

public class Inventory {
    private InventoryHeader header;
    private List<InventoryLine> lines;
}

public class InventoryHeader {
    private String date;
    private boolean isCurrent;
}


public class InventoryLine {
    private String itemName;
    private int quantity;
}

和以下 CSV(使用 ',' 作为分隔符,但为了可见性,我在这里使用了空格):

IH    2007-06-05    false
IL    Watch         7
IL    Flower Pot    9
IL    Chicken Wing  29
IH    2010-07-30    true
IL    Cable         200
IL    Fish Tank     87

在这种情况下,“IH”表示该行是库存标题,“IL”表示它是库存行。库存标题后面的库存行行仅与该库存有关。Inventory 对象的结尾由新的清单标题行或文件的结尾表示。

我想把它解析成一个列表。解析单个 Inventory 对象很简单,只需在第 0 列添加一个 ValueSwitch,为 InventoryHeader 和 InventoryLine 创建一个 BeanListProcessor 并将结果添加到新的 Inventory 对象。

使用上述方法,我们将获得标题和行的列表,但是如何知道哪些行对应于哪些标题?

4

1 回答 1

0

图书馆的作者在这里。检查此示例,因为它似乎与您的情况非常相似。

您需要覆盖您的rowProcessorSwitched方法InputValueSwitch才能知道解析器何时开始处理不同的行格式。

像这样的东西:

public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
    if(from == inventoryLineProcessor){ // stopped processing inventory lines and will process some other row.
        List<InventoryLine> inventoryLines = inventoryLineProcessor.getBeans();

        //now assign a copy of this list to the last InventoryHeader you have
    }
}

希望这可以帮助。

于 2018-08-22T06:03:14.750 回答