XMLUtil
package Testlink; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; //定义操作XML文件的方法
public class XMLUtil {
//定义工作目录
private File workspaceFile; //定义所有文件对象
private File dirFile; //定义buildXML文件夹
private File buildXMLDirectory; //html文件夹
private File htmlFileDirectory; //工作目录路径
private String workspacePath; //buildXML文件夹路径
private String buildXMLDirectoryPath; //定义build.xml文件路ing
private String buildXMLFilePath; //html文件夹路径
private String htmlFileDirectoryPath; //tap文件路径
private String tapPath; //测试结果节点
private List<Node> testResult = new ArrayList(); //样本节点
private List<Integer> sampleNumber = new ArrayList(); //此方法用户改变build.xml文件
public void BuildXMLChange() {
//获取工作目录路径
workspaceFile = new File("");
workspacePath = workspaceFile.getAbsolutePath(); //获取工作目录下所有文件
this.dirFile = new File(workspacePath);
File[] files = dirFile.listFiles(); //定义buildXML文件夹路径
buildXMLDirectoryPath = workspacePath + "\\BuildXML"; //获取buildXML文件夹所有文件
buildXMLDirectory = new File(buildXMLDirectoryPath);
File[] xmlFiles = this.buildXMLDirectory.listFiles(); //获取build.xml文件路径
buildXMLFilePath = buildXMLDirectoryPath + "\\build.xml"; if (files.length != 0) { File[] arrayOfFile1;
int j = (arrayOfFile1 = files).length; for (int i = 0; i < j; i++) { File file = arrayOfFile1[i]; if (file.getName().equals("BuildXML")) { if (xmlFiles.length != 0) { File[] arrayOfFile2;
int m = (arrayOfFile2 = xmlFiles).length; for (int k = 0; k < m; k++) {
File xmlFile = arrayOfFile2[k];
if (xmlFile.getName().equals("build.xml")) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
//读取xml
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("file:///" + buildXMLFilePath);
NodeList nodeList = document.getElementsByTagName("property"); Node property = nodeList.item(0); NamedNodeMap nodeMap = property.getAttributes();
Node node = nodeMap.getNamedItem("value");
node.setTextContent(workspacePath); //写入XML
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = factory.newTransformer();
former.setOutputProperty("encoding", "GBK");
try {
former.transform(new DOMSource(document),
new StreamResult(new File(buildXMLFilePath)));
} catch (TransformerException e) {
e.printStackTrace();
}
System.out.println("build.xml文件不存在");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
}
}
}
} else {
System.out.println("BuildXML文件夹为空");
}
}
}
} else {
System.out.println("文件夹内容为空");
}
} public void XMLToTAP() {
workspaceFile = new File("");
workspacePath = workspaceFile.getAbsolutePath(); dirFile = new File(workspacePath); File[] files = dirFile.listFiles(); htmlFileDirectoryPath = (workspacePath + "\\XMLFolder"); htmlFileDirectory = new File(htmlFileDirectoryPath); File[] htmlFiles = htmlFileDirectory.listFiles(); tapPath = (workspacePath + "\\TAPFolder");
if (files.length != 0) {
File[] arrayOfFile1;
int j = (arrayOfFile1 = files).length;
for (int i = 0; i < j; i++) {
File file = arrayOfFile1[i];
if (file.getName().equals("XMLFolder")) {
if (htmlFiles.length != 0) {
File[] arrayOfFile2;
int m = (arrayOfFile2 = htmlFiles).length;
for (int k = 0; k < m; k++) {
File xmlFile = arrayOfFile2[k];
if (xmlFile.getName().endsWith(".xml")) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFile); Element rootElement = document.getDocumentElement(); NodeList nodeList = rootElement.getChildNodes();
if (rootElement.hasChildNodes()) {
for (int x = 0; x < nodeList.getLength(); x++) {
if (nodeList.item(x).getNodeType() == 1) {
this.testResult.add(nodeList.item(x));
}
}
} else {
System.out.println("接口测试结果为空");
}
for (int y = 0; y < this.testResult.size(); y++) {
if (((Node) this.testResult.get(y)).getNodeName().equals("sample")) {
this.sampleNumber.add(Integer.valueOf(y));
}
}
for (int z = 0; z < this.sampleNumber.size(); z++) {
NamedNodeMap namedNodeMap = ((Node) this.testResult
.get(((Integer) this.sampleNumber.get(z)).intValue())).getAttributes();
if (namedNodeMap.getNamedItem("s").getNodeValue().equals("false")) {
String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue(); String[] fileContent = lbValue.split("-"); String fileNewPath = this.tapPath + "\\" + fileContent[0] + ".tap"; File fileNew = new File(fileNewPath); FileWriter fWriter = new FileWriter(fileNew, true);
BufferedWriter bWriter = new BufferedWriter(fWriter);
bWriter.write("TAP version 13");
bWriter.newLine();
bWriter.write("1..1");
bWriter.newLine();
String resultStr = "not ok 1";
bWriter.write(resultStr);
bWriter.flush();
bWriter.close();
fWriter.close();
} else {
String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue(); String[] fileContent = lbValue.split("-"); String fileNewPath = this.tapPath + "\\" + fileContent[0] + ".tap"; File fileNew = new File(fileNewPath); FileWriter fWriter = new FileWriter(fileNew, true);
BufferedWriter bWriter = new BufferedWriter(fWriter);
bWriter.write("TAP version 13");
bWriter.newLine();
String resultStr = "1..1\nok 1";
fWriter.write(resultStr);
bWriter.flush();
bWriter.close();
fWriter.close();
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("xml文件不存在");
}
}
} else {
System.out.println("XML文件夹为空");
}
}
}
} else {
System.out.println("文件夹内容为空");
}
}
}
XMLUtil的更多相关文章
- 上次遗留下来的XMLUtil的问题
·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...
- java实现xml格式与javabean之间的转换XmlUtil类
XmlUtil类:不多说,直接撸代码: /** * java 转换成xml * @Title: toXml * @Description: TODO * @param obj 对象实例 * @retu ...
- XStream 工具类 [ XmlUtil ]
pom.xml <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId> ...
- xmlUtil 解析 创建
http://yangzi09150915.blog.163.com/blog/static/32953487201072911410398/ package com.aibi.cmdc.webSer ...
- xml文档的解析并通过工具类实现java实体类的映射:XML工具-XmlUtil
若有疑问,可以联系我本人微信:Y1141100952 声明:本文章为原稿,转载必须说明 本文章地址,否则一旦发现,必追究法律责任 1:本文章显示通过 XML工具-XmlUtil工具实现解析soap报文 ...
- 反射 XMLUtil
package com.dys.util; import java.beans.Introspector; import java.beans.PropertyDescriptor; import j ...
- 工厂方法模式——创建型模式02
1. 简单工厂模式 在介绍工厂方法模式之前,先介绍一下简单工厂模式.虽然简单工厂模式不属于GoF 23种设计模式,但通常将它作为学习其他工厂模式的入门,并且在实际开发中使用的也较为频繁. (1 ...
- 复杂的xml转化为java实体
一.样例一: 以根据订单号向支付宝查询支付是否成功为例(成功信息) 失败信息: <?xml version="1.0" encoding="utf-8"? ...
- XStream简单入门
简单的讲,XStream 涉及的就五个知识点:详情参考 官网 混叠,注解,转换器,对象流和操作json! 下面就用几个简单的例子来实现上述五个知识点! 基本步骤: 第1步:创建XStream对象. 通 ...
随机推荐
- 四、robotframework生成几种随机数
1.random()生成0<=n<1之间的随机实数--它会生成一个随机的浮点数,范围是在0.0~1.0之间.: ${num} evaluate random.random() ra ...
- Spring Cloud Stream 使用延迟消息实现定时任务(RabbitMQ)
应用场景 通常在应用开发中我们会碰到定时任务的需求,比如未付款订单,超过一定时间后,系统自动取消订单并释放占有物品. 许多同学的第一反应就是通过spring的schedule定时任务轮询数据库来实现, ...
- 读Dubbo源码,学习SPI
核心类 ExtensionLoader 使用方法 定义接口,使用@SPI标记 @SPI("impl1") public interface SimpleExt { // @Adap ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_10 打印流_1_打印流_概述和使用
system.out就是打印流 System的源码里面out参数就是一个PrintStream类型的 例如路径写的是E盘,.没有e盘这个路径就会抛出异常 用println直接写入的就是97 这个 ...
- 【HANA系列】SAP HANA 1.0 SPS 11 新特性
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA 1.0 SPS ...
- 应用安全-Web安全-子域名/相关域名
技巧 DNS解析记录 主站获取 单点登录接口 crossdomain.xml IP反查 通过HTTPS证书收集 DNS域传送搜集 联系人信息/邮箱反查域名 x-dns-prefetch-control ...
- python列表-使用
一.列表用于循环 1.for循环 2. in 和 not in 3.多重赋值
- locale报错,显示中文乱码
locale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAG ...
- <每日一题> Day8:CodeForces-996A.Hit the Lottery(贪心)
原题链接 因为数据太水,我直接一发暴力过了...... #include <cstdio> using namespace std; ] = {, , , , }; int main() ...
- document.domain location.hostname location.host
document.domain location.hostname location.host :https://www.cnblogs.com/shd-study/p/103031 ...