我试图使用Java和MetaModel API查询一个MetaModel API文件,但没有成功。这个API对于.XLS文件很容易,但是对于.CSV,我很难理解,因为没有表名来查询这个查询。有人能对如何做到这一点提供任何提示或洞察力吗?
public static Object[][] getCsvData( File csvFile )
{
CsvConfiguration conf = new CsvConfiguration( 1, true, false );
DataContext ctx = DataContextFactory.createCsvDataContext( csvFile, conf );
// CsvDataSet dataSet = csvContext.query().
// .selectAll()
// .where("run").eq("Y")
// .execute();
// List<Row> rows = dataSet.toRows();如果您想尝试我的代码,请在这里查看我的GitHub项目,这是正在进行中的概念的证明(使用一个工作的Excel示例)。
发布于 2014-02-19 09:38:42
你可以为CSV这样做
public static Object[][] getCsvData(File csvFile) {
CsvConfiguration conf = new CsvConfiguration(1);
DataContext csvContext = DataContextFactory.createCsvDataContext(
csvFile, conf);
Schema schema = csvContext.getDefaultSchema();
Table[] tables = schema.getTables();
Table table = tables[0];
DataSet dataSet = csvContext.query().from(table).selectAll().where("run").eq("Y").execute();
List<Row> rows = dataSet.toRows();
Object[][] myArray = new Object[rows.size()][2];
int i = 0;
SelectItem[] cols = rows.get(0).getSelectItems();
for (Row r : rows) {
Object[] data = r.getValues();
for (int j = 0; j < cols.length; j++) {
if (data[j] == null)
data[j] = ""; // force empty string where there are NULL
// values
}
myArray[i][0] = cols;
myArray[i][1] = data;
i++;
}
logger.info("Row count: " + rows.size());
logger.info("Column names: " + Arrays.toString(cols));
return myArray;
}我想出来是为了XmlDataProvider你可以这样做
@Test
public void testXML() {
XmlSaxTableDef employeeTableDef = new XmlSaxTableDef(
"/root/organization/employees/employee", new String[] {
"/root/organization/employees/employee/name",
"/root/organization/employees/employee/gender",
"index(/root/organization)"});
XmlSaxTableDef organizationTableDef = new XmlSaxTableDef(
"/root/organization", new String[] {
"/root/organization/name",
"/root/organization@type" });
DataContext dc = new XmlSaxDataContext(xmlFile, employeeTableDef,
organizationTableDef);
Table employeeTable = dc.getTableByQualifiedLabel("/employee");
Column fk = employeeTable.getColumnByName("index(/root/organization)");
Column empName = employeeTable.getColumnByName("/name");
Table organizationTable = dc.getTableByQualifiedLabel("/organization");
Column orgId = organizationTable.getColumnByName("row_id");
Column orgName = organizationTable.getColumnByName("/name");
Query q = dc.query().from(employeeTable)
.innerJoin(organizationTable).on( fk, orgId )
.select(empName).as("employeename")
.select(orgName).as("companyname").toQuery();
DataSet ds = dc.executeQuery(q);
List<Row> rows = ds.toRows();
for (Row r : rows) {
System.out.println(Arrays.deepToString(r.getValues()));
}
}如果你面临任何问题,请告诉我:)
https://stackoverflow.com/questions/21870739
复制相似问题