測试之路2——对照XML文件1

时间:2023-03-09 00:30:08
測试之路2——对照XML文件1

才来几天,老大又给了我一个新的任务。不像曾经的建100个任务project那么坑爹,却还是顿时让我感觉压力山大。

由于在这之前,我改了他写的例程,用于生成新的任务项目,事实上任务项目就是通过XML文件进行參数传递,底层早已经封装好了。可是问题出来了,你新建任务须要传过去一个XML文件,可是server端生成任务还会返回一个XML文件,而我对于server端并不了解,我仅仅知道server生成了一个XML文件,可是它生成的XML中的參数是否和我传过去的參数同样?

所以老大要我写一个方法,去比对这两份XML文件。(自己传到server和server生成并返回的)

以后我都会称自己传到server为源文件,server生成返回的为目标文件

这一下倒是难到了我,由于第一,我对xml文件不熟;第二,我没有考虑好怎样去对照xml文件。

首先,xml文件里都是以string字符串的形式写入,在java程序中,事实上就是对照string。可是我不知道该怎么去比对这两个字符串,是比对字符串长度?不可能,源文件的长度肯定小于目标文件,目标文件会生成相应的任务名称,id等參数,而这些在源文件里是缺省的。

这让我感到绝望,只是好在有万能的baidu:他告诉我,java中有非常多封装好的类,能够将xml文件直接进行转化,比方:DOM。

原来还能够这样,于是我又查了查Document类,略去基础的不说,直奔主题:

这个DOM类能够直接对xml文档操作,能够读取xml文件,并生成一个对象,这个对象就是xml文件的树形化(xml也是类似于树结构,dom相当于直接生成这个数结构),那么我就能够直接訪问这个对象,对这个对象进行操作,比方能够得到子节点,得到根文件夹……

那么怎么使用dom呢?直接上代码不解释:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; //须要引入的包
@Override
public boolean compare(String sourcePath, String targetPath) {
<span style="white-space:pre"> </span>boolean result=false;
<span style="white-space:pre"> </span>File sourceFile = new File(sourcePath);
<span style="white-space:pre"> </span>File targetFile = new File(targetPath);
<span style="white-space:pre"> </span>Document sourceDoc = builder.parse(sourceFile);
<span style="white-space:pre"> </span>Document targetDoc = builder.parse(targetFile);
<span style="white-space:pre"> </span>simpleCompare(sourceDoc, targetDoc);
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Document sourceDoc = builder.parse(sourceFile);
<span style="white-space:pre"> </span>Document targetDoc = builder.parse(targetFile); <span style="white-space:pre"> </span>// DebugUtil.formatXML(sourceDoc);
<span style="white-space:pre"> </span>simpleCompare(sourceDoc, targetDoc);//简单对照 <span style="white-space:pre"> </span>} catch (SAXException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return result;
}

上面代码大概就这么个意思,compare是个构造方法:方法内创建格式工厂,读取xml文件的路径,然后去解析这个xml文件,返回得到的Document对象(是一个树形的数据结构)。

那么实际就是操作sourceDoc和targetDoc这两个Document对象就能够了。

剩下那不是非常easy?于是,我找到了Document的方法:getChildNodes(),返回全部孩子的节点,以链表的形式,我当然能够觉得链表的名字就是xml标签名字,值为xml标签的值,所以我就能够进行对照了。于是又是洋洋洒洒一段代码。

public boolean simpleCompare(Document sourceDoc,Document targetDoc){
// Element sourceRoot = sourceDoc.getDocumentElement(); //得到源文件的根节点
Element targetRoot = targetDoc.getDocumentElement(); //得到目标文件的根节点 // HashSet<String> sourceSet =new HashSet<String>(); //创建哈希表
// HashSet<String> targetSet =new HashSet<String>();
// HashSet<String> sourceNames=getNodeNames(sourceRoot,sourceSet); //一个创建哈希表函数
// HashSet<String> targetNames=getNodeNames(targetRoot,targetSet); NodeList sourceNodes = sourceDoc.getChildNodes(); //得到源文件的孩子,为了进行对照
NodeList targetNodes = targetDoc.getChildNodes(); int sourceLength = sourceNodes.getLength();
int targetLength = targetNodes.getLength();
int i = 0,j = 0;
for (i = 0; i < sourceLength; i++) {
String name = sourceNodes.item(i).getNodeName();
String value = sourceNodes.item(i).getNodeValue();
for (j = 0; j < targetLength; j++) {
if (name.equals(targetNodes.item(j).getNodeName())) {
if (value.equals(sourceNodes.item(j).getNodeValue())) { }
else {
return false;//假设值不正确,说明对照错误
}
}
}
if(j == targetLength){ //假设一直没有在目标文件里找到,说明对照错误
return false;
}
}
return true;
}

本以为这样都就能够完毕对照的任务了,可是当我拿两份内容一样的xml文件去对照,console里返回的都是false,这让我变得非常忧伤。

至于为什么,肯定是哪里出现故障了,所以我要输出来看一看,究竟出了什么错。