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

前端之家收集整理的这篇文章主要介绍了测试之路2——对比XML文件1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

才来几天,老大又给了我一个新的任务。不像以前的建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呢?直接上代码不解释:


  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.parsers.ParserConfigurationException;
  4.  
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Element;
  7. import org.w3c.dom.Node;
  8. import org.w3c.dom.NodeList;
  9. import org.xml.sax.SAXException; //需要引入的包
  1. @Override
  2. public boolean compare(String sourcePath,String targetPath) {
  3. <span style="white-space:pre"> </span>boolean result=false;
  4. <span style="white-space:pre"> </span>File sourceFile = new File(sourcePath);
  5. <span style="white-space:pre"> </span>File targetFile = new File(targetPath);
  6. <span style="white-space:pre"> </span>Document sourceDoc = builder.parse(sourceFile);
  7. <span style="white-space:pre"> </span>Document targetDoc = builder.parse(targetFile);
  8. <span style="white-space:pre"> </span>simpleCompare(sourceDoc,targetDoc);
  9. <span style="white-space:pre"> </span>try {
  10. <span style="white-space:pre"> </span>Document sourceDoc = builder.parse(sourceFile);
  11. <span style="white-space:pre"> </span>Document targetDoc = builder.parse(targetFile);
  12.  
  13. <span style="white-space:pre"> </span>// DebugUtil.formatXML(sourceDoc);
  14. <span style="white-space:pre"> </span>simpleCompare(sourceDoc,targetDoc);//简单对比
  15.  
  16.  
  17. <span style="white-space:pre"> </span>} catch (SAXException e) {
  18. <span style="white-space:pre"> </span>e.printStackTrace();
  19. <span style="white-space:pre"> </span>} catch (IOException e) {
  20. <span style="white-space:pre"> </span>e.printStackTrace();
  21. <span style="white-space:pre"> </span>}
  22. <span style="white-space:pre"> </span>return result;
  23. }

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

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

剩下那不是很简单?于是,我找到了Document的方法:getChildNodes(),返回所有孩子的节点,以链表的形式,我当然可以认为链表的名字就是xml标签名字,值为xml标签的值,所以我就可以进行对比了。于是又是洋洋洒洒一段代码

  1. public boolean simpleCompare(Document sourceDoc,Document targetDoc){
  2. // Element sourceRoot = sourceDoc.getDocumentElement(); //得到源文件的根节点
  3. Element targetRoot = targetDoc.getDocumentElement(); //得到目标文件的根节点
  4. // HashSet<String> sourceSet =new HashSet<String>(); //创建哈希表
  5. // HashSet<String> targetSet =new HashSet<String>();
  6. // HashSet<String> sourceNames=getNodeNames(sourceRoot,sourceSet); //一个创建哈希表函数
  7. // HashSet<String> targetNames=getNodeNames(targetRoot,targetSet);
  8. NodeList sourceNodes = sourceDoc.getChildNodes(); //得到源文件的孩子,为了进行对比
  9. NodeList targetNodes = targetDoc.getChildNodes();
  10. int sourceLength = sourceNodes.getLength();
  11. int targetLength = targetNodes.getLength();
  12. int i = 0,j = 0;
  13. for (i = 0; i < sourceLength; i++) {
  14. String name = sourceNodes.item(i).getNodeName();
  15. String value = sourceNodes.item(i).getNodeValue();
  16. for (j = 0; j < targetLength; j++) {
  17. if (name.equals(targetNodes.item(j).getNodeName())) {
  18. if (value.equals(sourceNodes.item(j).getNodeValue())) {
  19. }
  20. else {
  21. return false;//如果值不对,说明对比错误
  22. }
  23. }
  24. }
  25. if(j == targetLength){ //如果一直没有在目标文件中找到,说明对比错误
  26. return false;
  27. }
  28. }
  29. return true;
  30. }

本以为这样都就可以完成对比的任务了,但是当我拿两份内容一样的xml文件去对比,console里返回的都是false,这让我变得很忧伤。

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

猜你在找的XML相关文章