当前位置:网站首页>Dom4j解析XML文件,处理来至XML的数据信息
Dom4j解析XML文件,处理来至XML的数据信息
2022-07-22 18:10:00 【洛央虲】
XML文件 <?xml version="1.0" encoding="UTF-8"?>
<datasets name="ds1">
<locators name="LoginSetMet">
<locator name="loginLocator" by="id">login</locator>
<locator name="pwdLocator" by="name">tl_password</locator>
<locator name="submitLocator" by="xpath">//input[@name='login_submit']
</locator>
</locators>
</datasets>
使用Dom4j进行解析
package com.auto.demo1;
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
/** * dom4j java XML开源解析工具包 * * @author Monica * */
public class LocatorPaser {
private Document doc;// 定义Document文档对象
// 默认的构造方法中完成对XML文件的解析
public LocatorPaser(){
InputStream in = getInputFileAsStream(this.getClass());
SAXReader reader = new SAXReader();//得到解析器
try{
doc = reader.read(in);//解析文档
}catch(DocumentException e){
e.printStackTrace();
}
}
private InputStream getInputFileAsStream(Class<? extends LocatorPaser> testClass) {
// TODO Auto-generated method stub
String packageName = "";
if(testClass.getPackage()!=null){
packageName = testClass.getPackage().getName() + ".";
}
String resourceBase = (packageName + testClass.getSimpleName()).replace('.', '/');
//得到当前类的完整路径
String resourceName;
ClassLoader classLoader = testClass.getClassLoader();
resourceName = resourceBase + ".xml";
//XML文件与当前类同名,且在同一目录下
System.out.println("Searching for default input file:"+resourceName);
return classLoader.getResourceAsStream(resourceName);
//返回同名XML文件资源的获取
}
public String getLocator(String name){
String locator = null;
//查找到"datasets"标签
Element datasets = (Element) this.doc.selectSingleNode("//datasets");
//开始定位串解析
for(Iterator d = datasets.elementIterator("locators");d.hasNext();){
Element data = (Element)d.next();
for(Iterator e = data.elementIterator("locator");e.hasNext();){
Element elem = (Element)e.next();
System.out.println(elem);
//获取当前结点的值
String value = elem.valueOf(".");
System.out.println(value);
//获取name属性值
String nameString = elem.valueOf("@name");
System.out.println("\n'"+nameString+"'");
if(name.equalsIgnoreCase(nameString)){
if(!elem.valueOf("@by").isEmpty()){
locator = elem.valueOf("@by")+"="+value;//将by属性值与当前节点值进行拼接,如”xpath=//input[@name='login_submit']"
}
break;//完成解析,跳出循环
}
}
}
return locator;//返回定位串或者null
}
@Test
public void testPaser(){
String locator = "loginLocator";
long curTime = System.currentTimeMillis();
String resultString = this.getLocator(locator);
long delta = System.currentTimeMillis()-curTime;
System.out.println("\n"+locator+"='"+resultString+"'\nTime used(ms)::"+delta);
}
}
运行结果:
单元测试通过!
控制台输出:
Searching for default input file:com/auto/demo1/LocatorPaser.xml
[email protected]77f03bb1 [Element: <locator attributes: [[email protected]233c0b17 [Attribute: name name value "loginLocator"], [email protected]63d4e2ba [Attribute: name by value "id"]]/>]
login
'loginLocator'
loginLocator='id=login'
Time used(ms)::34
边栏推荐
- web端接口数据一键复制导入postman
- Test case: phone number
- Test point, summary of the first day
- beanshell处理json的技巧
- Easy to understand, master interface automation-01
- 爬取网页动态加载的评论
- Day04 -- Installation of Zen path
- [after the sit environment test is passed, deploy the pre environment, and there are still reasons for the existence of bugs]
- PTP北斗授时服务器(卫星时钟服务器)助力高考信息化系统
- Selenium基础知识 自动搜索
猜你喜欢
随机推荐
关于如何在终端设置有颜色的字体
Millisecond upload batch attachments
Linked list 3 Analysis of the optimal linked list structure: leading two-way circular linked list
Teach you how to interview and keep away from interview fear
学习Amber T3.3:隐式溶剂模型(GB)的MD
Process of using Zen
關於後臺掛載,進程管理的學習
安装和登录登录
Test point, summary of the first day
Jmeter上传和下载文件
Ie settings - solve the problem of uploading upgrade packages with drive letter paths
用jmeter对抽奖接口进行抽奖概率分析测试
Day02 test case
[after the sit environment test is passed, deploy the pre environment, and there are still reasons for the existence of bugs]
金字塔型自动化的利弊
Software testing is divided by stages: unit testing, integration testing, system testing, acceptance testing
IE浏览器跨域设置
Day 2 summary and test case operation
Software service function process
深入浅出掌握接口自动化







![[original] software test example guidance](/img/74/4d1e6cd537d4948dace95633140939.png)

