public class BaseXmlData {
private List<Map<String,Map>> l;
private String xmlPath=null;
public BaseXmlData(String xmlDataPath) {
xmlPath=xmlDataPath;
ParserXml p = new ParserXml();
l = p.parser3Xml(new File(xmlPath).getAbsolutePath());
}
public Object[][] getData(String methodName) {
return addList(methodName);
}
public Object[][] getData(int beginRowNum, int endRowNum) {
Object[][] data = null;
data = addList2(beginRowNum, endRowNum);
return data;
}
public Map<String,Map> list2Map(List<Map<String,Map>> list){
Map<String,Map> map= new HashMap<String, Map>();
Set set=null;
for(int i=0;i<list.size();i++){
set=list.get(i).entrySet();
Map.Entry[] entries = (Map.Entry[])set.toArray(new Map.Entry[set.size()]);
map.put(entries[0].getKey()+"",(Map)entries[0].getValue());
}
return map;
}
public Object[][] addList(String method) {
Object[][] data = new Object[1][1];
data[0][0]= list2Map(l).get(method);
return data;
}
public Object[][] addList2(int beginRowNum, int endRowNum) {
if(endRowNum>l.size()){
endRowNum=l.size();
System.out.println("endRowNum行数不能大于最大行数!故endRowNum返回最大行数:"+l.size());
}
Object[][] data = new Object[endRowNum-beginRowNum+1][1];
List<Map<String, Map>> list=new ArrayList<Map<String, Map>>();
for(int i=0;i<=endRowNum-beginRowNum;i++){
list.add(l.get(i));
}
Map map=list2Map(list);
Set<Map.Entry<String, Map>> entryseSet=map.entrySet();
int j=0;
for (Map.Entry<String, Map> entry:entryseSet) {
data[j][0]=entry.getValue();
j++;
}
return data;
}
}
public class ParserXml {
public List<Map<String,Map>> parser3Xml(String fileName) {
File inputXml = new File(fileName);
List<Map<String,Map>> list=new ArrayList();
int count = 1;
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext();) {
Element employee = (Element) i.next();
Map map = new HashMap();
Map tempMap = new HashMap();
for (Iterator j = employee.elementIterator(); j.hasNext();) {
Element node = (Element) j.next();
tempMap.put(node.getName(), node.getText());
}
map.put(employee.getName(), tempMap);
list.add(map);
}
} catch (DocumentException e) {
System.out.println(e.getMessage());
}
return list;
}
}
excel:
package app;
/**
* Created by shenmajr on 2017/7/10.
*/
public Object[][] getData() {
return getData( 0);
}
public Object[][] getData(int rowNum) {
Object[][] data = null;
data = addList( rowNum);
return data;
}
public Object[][] getData(int beginRowNum, int endRowNum) {
Object[][] data = null;
data = addList2(beginRowNum, endRowNum);
return data;
}
public Object[][] addList(int rowNum) {
Object[][] data = new Object[1][1];
int totalCol = colNum();
Map map = new HashMap();
for (int i = 0; i < totalCol; i++) {
map.put(readexcel(0, i), readexcel(rowNum, i));
}
data[0][0] = map;
return data;
}
public Object[][] addList2( int beginRowNum, int endRowNum) {
Object[][] data = new Object[endRowNum - beginRowNum + 1][1];
int totalCol = colNum();
int num = 0;
for (int r = beginRowNum; r <= endRowNum; r++) {
Map map = new HashMap();
for (int i = 0; i < totalCol; i++) {
// System.out.println(dataDriver.readexcel(0,i)+","+dataDriver.readexcel( r ,i));
map.put(readexcel(0, i), readexcel(r, i));
}
data[num][0] = map;
num++;
}
return data;
}
public int rowNum() {
int row = 0;
try {
this.sheet = this.wb.getSheet(this.sheetName);
row = this.sheet.getLastRowNum();
} catch (NullPointerException var3) {
System.out.println("No such sheetname:" + this.sheetName + " ! Excel file in " + this.input + ":");
}
return row;
}
public int colNum() {
this.sheet = this.wb.getSheet(this.sheetName);
this.row = this.sheet.getRow(0);
int col = this.row.getPhysicalNumberOfCells();
return col;
}
public String readexcel(int rowNum, int colNum) {
int lastrow = 0;
String cellValue = null;
this.row = this.sheet.getRow(rowNum);
lastrow = this.sheet.getLastRowNum();
if (rowNum > lastrow) {
rowNum = lastrow;
System.out.println("行数大于最大行数,故使用最大行数!");
}
int totalCol = this.row.getPhysicalNumberOfCells();
if (colNum > totalCol) {
colNum = totalCol;
System.out.println("列数大于最大行数,故使用最大列数!");
}
cellValue = this.sheet.getRow(rowNum).getCell(colNum).toString();
return cellValue;