码迷,mamicode.com
首页 > 其他好文 > 详细

测试之路2——对比XML文件1

时间:2014-06-01 05:26:43      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

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

因为在这之前,我改了他写的例程,用于生成新的任务项目,其实任务项目就是通过XML文件进行参数传递,底层早已经封装好了。但是问题出来了,你新建任务需要传过去一个XML文件,但是服务器端生成任务还会返回一个XML文件,而我对于服务器端并不了解,我只知道服务器生成了一个XML文件,但是它生成的XML中的参数是否和我传过去的参数相同?

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

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


这一下倒是难到了我,因为第一,我对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对象就可以了。

剩下那不是很简单?于是,我找到了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,这让我变得很忧伤。

至于为什么,肯定是哪里出现问题了,所以我要输出来看一看,到底出了什么错。


测试之路2——对比XML文件1,布布扣,bubuko.com

测试之路2——对比XML文件1

标签:des   c   style   class   blog   code   

原文地址:http://blog.csdn.net/f4d3s2a10p/article/details/27217829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!