51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1068|回复: 1
打印 上一主题 下一主题

[在学] Dom解析XML和excel文件

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-1-29 11:14:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
第一节课时老师要将XML和excel文件解析并转化成Object[][]对象,这里有代码吗?之前学的java基础课程里没有这些,现在百度网上的都看不懂,不知道该怎么解析


在学课程:
自动化测试之Testng高级使用
http://www.atstudy.com/course/1009
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2019-1-29 11:22:50 | 只看该作者
可以用如下方法转化,getData方法就是具体实现:
public class DataDriver {
    public static Object[][] getExcelData(String fileName, String sheetName,int rowNum){
        BaseExcelData excelData=new BaseExcelData( fileName, sheetName);
        return excelData.getData(rowNum);
    }
    public static Object[][] getExcelData(String fileName, String sheetName,int beginRow,int endRow){
        BaseExcelData excelData=new BaseExcelData( fileName, sheetName);
        return excelData.getData(beginRow,endRow);
    }
    public static Object[][] getXmllData(String xmlDataPath,String methodName){
        BaseXmlData xmlData=new BaseXmlData( xmlDataPath);
        return xmlData.getData( methodName);
    }
    public static Object[][] getXmllData(String xmlDataPath,int beginRow,int endRow){
        BaseXmlData xmlData=new BaseXmlData( xmlDataPath);
        return xmlData.getData(beginRow,endRow);
    }
}
getData方法实现:

xml:

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.
*/

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BaseExcelData {
    private String input = null;
    private String sheetName = null;
    private Workbook wb;
    private Sheet sheet;
    private Row row;
    List<String> title = new ArrayList();

    public BaseExcelData(String fileName, String sheetName) {
        input = fileName;
        this.sheetName = sheetName;
        try {
            this.wb = new XSSFWorkbook(this.input);
        } catch (IOException e) {
            System.out.println("DataPath invalid:" + this.input);
        }
        System.out.println("文件路径:" + this.input);
        System.out.println("sheetName:" + this.sheetName);
    }

    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;

    }
...................
由于字数限制,完整代码在课后作业的参考答案里有的,可以到课后作业那边下载

回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-5 08:28 , Processed in 0.072343 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表