您的位置:首页 > 其它

使用xpath新增、查找、删除、上下移动节点

2016-06-18 21:46 519 查看
最近在一个测试工具的项目开发中,需要用到swt的tree控件构建一棵树来组织测试用例,swt本身提供了tree控件来开发树形结构,我所要做的就是把数据按照业务逻辑关系组织成一个xml文件,通过解析xml来构建这棵树,xml代码如下:

<?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;
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: