使用xpath新增、查找、删除、上下移动节点
2016-06-18 21:46
519 查看
最近在一个测试工具的项目开发中,需要用到swt的tree控件构建一棵树来组织测试用例,swt本身提供了tree控件来开发树形结构,我所要做的就是把数据按照业务逻辑关系组织成一个xml文件,通过解析xml来构建这棵树,xml代码如下:
1、向xml中创建节点
2、删除节点
首先要找到该节点,然后删除。代码如下
3、上下移动节点
一种是移动叶子节点,一种是移动父节点包括它的子节点。移动叶子节点需要找到它的兄弟节点,并交换节点内容,这种比较简单。移动父节点及其叶子节点时先交换父节点的兄弟节点,之后,将父节点下的子节点拷贝给一个临时变量,并删除这些子节点,再将它的兄弟节点下的子节点内容赋给该父节点,再把临时变量里保存的子节点赋给该父节点的兄弟节点。
1)获取选中节点的兄弟节点----移动叶子节点
4、生成一个临时节点用于保存要移动的父节点下的子节点
剩下的工作就是交换两个父节点下的子节点,这里就不细说了。以上列出的节点查找、删除、包括这里没有提到的重命名都需要用到下面两个重要的方法:
5、使用xpath查找节点、节点集合
<?xml version="1.0" encoding="GB2312" standalone="no"?> <configs> <project name="测试1"> <report name="测试报告"/> <testsuite name="newSuite1"> <testcase name="newCase1"> <teststep>newStep7</teststep> <teststep>newStep00</teststep> <teststep>newStep1</teststep> <teststep>newStep2</teststep> </testcase> <testcase name="newCase5"> <teststep>copy1TonewStep00</teststep></testcase> </testsuite> </project> <project name="测试2"> <report name="测试报告"/> </project> </configs>
1、向xml中创建节点
try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(filePath); //report.html目录 File reportFile = new File(StringContent.TESTER_BASE_PATH + "\\" + projectName + "\\" + "report.html"); NodeList rootList = document.getElementsByTagName("configs"); for (int i = 0; i < rootList.getLength(); i++) { Node prjNode = rootList.item(i); Element project_elmt = document.createElement("project"); Attr name_s = document.createAttribute("name"); name_s.setValue(projectName); project_elmt.setAttributeNode(name_s); prjNode.appendChild(project_elmt); //把report.html挂在工程目录下 Element report_elmt = document.createElement("report"); Attr name_r = document.createAttribute("name"); name_r.setValue(reportFile.getName()); name_r.setValue("测试报告"); report_elmt.setAttributeNode(name_r); project_elmt.appendChild(report_elmt); } saveXML(document);
2、删除节点
首先要找到该节点,然后删除。代码如下
public static void deleteTestStep(String prjNm, String suiteNm,String testylNm,String stepNm){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document xmldoc = db.parse(new File(filePath)); Element root = xmldoc.getDocumentElement(); //选取STEP节点 if((prjNm != null && prjNm != "") && (suiteNm != null && suiteNm != "") && (testylNm != null && testylNm != "") && (stepNm != null && stepNm != "")){ NodeList nodes = selectNodes("/configs/project[@name='"+prjNm+"']" +"/testsuite/testcase/teststep", root); for(int i = 0;i<nodes.getLength();i++){ if(nodes.item(i).getTextContent().equals(stepNm)){ nodes.item(i).getParentNode().removeChild(nodes.item(i)); } } saveXML(xmldoc); }else{ System.out.println("node is not exists"); } catch (Exception e) { e.printStackTrace(); } } }
3、上下移动节点
一种是移动叶子节点,一种是移动父节点包括它的子节点。移动叶子节点需要找到它的兄弟节点,并交换节点内容,这种比较简单。移动父节点及其叶子节点时先交换父节点的兄弟节点,之后,将父节点下的子节点拷贝给一个临时变量,并删除这些子节点,再将它的兄弟节点下的子节点内容赋给该父节点,再把临时变量里保存的子节点赋给该父节点的兄弟节点。
1)获取选中节点的兄弟节点----移动叶子节点
/** * 获取选中结点的兄弟结点--step * @param nodeList * @param node * @return */ private static void getNextSibling(NodeList nodeList,String nodeValue,int flag){ boolean isLoop = true; String tmp = ""; Node nextSibling = null; if(flag == 2){ //step下移 for (int i = 0; i < nodeList.getLength(); i++) { Node curNode = nodeList.item(i); if(curNode.getNodeType() == Node.ELEMENT_NODE){ if (nodeList.item(i).getTextContent().equals(nodeValue)) { while (isLoop) { Node curNode_1 = nodeList.item(i); if(curNo_1 != null&& curNode_1.getNextSibling() != null && curNode_1.getNextSibling().getNodeType() == Node.ELEMENT_NODE) { nextSibling = curNode_1.getNextSibling(); //交换step内容 tmp = nextSibling.getTextContent();curNode.setTextContent(tmp); nextSibling.setTextContent(nodeValue); isLoop = false; } if (i < nodeList.getLength()) { i++; } else { isLoop = false; } } break; } } } }else{ //step上移 for (int i = nodeList.getLength() - 1; i >= 0; i--) { Node curNode = nodeList.item(i); if(curNode.getNodeType() == Node.ELEMENT_NODE){ if (nodeList.item(i).getTextContent().equals(nodeValue)) { while (isLoop) { Node curNode_1 = nodeList.item(i); if (curNode_1 != null && curNode_1.getPreviousSibling() != null && curNode_1.getPreviousSibling().getNodeType() == Node.ELEMENT_NODE) { nextSibling = curNode_1.getPreviousSibling(); //交换step内容 tmp = nextSibling.getTextContent(); curNode.setTextContent(tmp); nextSibling.setTextContent(nodeValue); isLoop = false } if (i >= 0) { i--; } else { isLoop = false; } } break; } } } } }
4、生成一个临时节点用于保存要移动的父节点下的子节点
Element node_tmp = xmldoc.createElement("testcase"); //创建一个临时结点用于保存step node_tmp.setAttribute("name", "test"); //把case下step复制给新结点 for (int i = 0; i < stepnode1_list.getLength(); i++) { Element n1 = xmldoc.createElement("teststep"); n1.setTextContent(stepnode1_list.item(i).getTextContent()); node_tmp.appendChild(n1); } //删除curCase下所有step for (int i = 0; i < stepnode1_list.getLength(); i++) { stepnode1_list.item(i).getParentNode().removeChild(stepnode1_list.item(i)); }
剩下的工作就是交换两个父节点下的子节点,这里就不细说了。以上列出的节点查找、删除、包括这里没有提到的重命名都需要用到下面两个重要的方法:
5、使用xpath查找节点、节点集合
/** * 查找节点,并返回第一个符合条件节点 * @param express * @param source * @return */ public staticNode selectSingleNode(String express, Object source) {Node result = null; XPathFactory xpathFactory = XPathFactory.newInstance();XPath xpath = xpathFactory.newXPath(); try { result = (Node) xpath.evaluate(express, source, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; } /** * 查找节点,返回符合条件的节点集。 * @param express * @param source * @return */ public static NodeList selectNodes(String express, Object source) { NodeList result = null; XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; }
相关文章推荐
- 表空间及组成表空间的物理文件
- void类型及void指针
- 字节序两种类型的通俗理解
- Android实现卫星菜单(一)自定义属性及重写onMeasure、onLayout方法
- Hibernate不同DB的日期查询
- AJAX学习笔记--慕课网Ajax
- 牛腩视频总结(三)
- 350. Intersection of Two Arrays II
- python_day6学习笔记
- 附加题(二)——沈航软件工程期末附加作业
- VMWare 网络设置简介
- 没有所谓的人脉,只有更强的自己
- IOS性能调优系列:使用Allocation动态分析内存使用情况
- Zookeeper简介、安装与使用
- JavaSE入门学习51:多线程编程(二)
- 进度条15
- 第18章 行为型模式—迭代器模式
- 构建之法阅读笔记06
- Android App 版本更新实现
- Git Pro深入浅出(一)