你可以像這樣爲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/[email protected]" });
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()));
}
}
讓我知道,如果你面對任何問題:)
謝謝!我今晚會嘗試。我試圖使用「CsvDataContext」,它不工作,我注意到你也沒有使用它。 – djangofan
我今天也嘗試過多次使用CsvDataContext ...它沒有奏效... – Naren
謝謝。有效。你可以在這裏看到我的示例項目:https://github.com/djangofan/MetaModelExample。現在我正試圖讓XML數據提供者工作,然後完成我的POC。希望儘快解決這個問題,但這有點困難。 – djangofan