使用XPath的条件查询和多节点条件查询

时间:2021-08-15 22:24:45

首先是我们需要查询的xml文件autotest.xml

<autotests>
<autotest>
<manifest >
<url>ssh://abt-node1.sh.intel.com/manifest</url>
<branch>ciengine/testdata</branch>
</manifest>
<build>
<step>
<type>jenkins</type> <jenkinsProjectName>HelloWorld</jenkinsProjectName>
</build>
</autotest>
<autotest>
<manifest >
<url>ssh://abt-node1.sh.intel.com/manifest2</url>
<branch>ciengine/testdata</branch>
</manifest>
<build>
<step>
<type>jenkins</type> <jenkinsProjectName>HelloWorld2</jenkinsProjectName>
</step>
</build>
</autotest>
</autotests>

1、XPath的条件查询
这里我们需要通过url和branch节点的值确定对应的jenkinsProjectName

public static String getXmlValueByPath_jenkins (String xmlString, String url, String branch) {
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath XPath = xpathFactory.newXPath();
XPathExpression expr = null;
//下面这一句是关键
String line = "/autotests/autotest[manifest/url[text()='"+ url + "'] and manifest/branch[text()='" + branch + "']]/build/step/jenkinsProjectName";
try {
expr = XPath.compile(line);
} catch (XPathExpressionException e1) {
e1.printStackTrace();
}

InputSource source = new InputSource(new StringReader(xmlString));
String value = null;
try {
value = (String) expr.evaluate(source, XPathConstants.STRING);
} catch (XPathExpressionException e) {
e.printStackTrace();
}

return value;

}

String line=”“里面的内容解析:
首先我们要查找的是/autotests/autotest/build/step/jenkinsProjectName这一完整路径下的的value,但是我们这一有一条件就是需要先找点符合条件和的节点,然后取出其中节点的value,所以在上面的完整路径中加入我们需要的条件信息[manifest/url[text()=’ url ‘] and manifest/branch[text()=’ branch ‘]],因为节点和我们要查找路径上的节点是兄弟关系,所以要加”[ ]”,另外我们需要指定我们要求的条件,于是url[text()=’ url ‘]这样指定,text()表示节点的value,url是其值要加’ ‘单引号,如果条件是节点属性的值比如则用@type=’ ‘取其值,同时我们还可以使用 and 、or等连接词。

2、XPath的多节点条件查询
这里我们需要取出所有的节点包括其中的url和branch的value

public static List getXmlValueByPath_manifest(String xmlString) {
List <Manifest1> list = new ArrayList();
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath XPath = xpathFactory.newXPath();
XPathExpression expr = null;
String line = "//manifest";

try {
expr = XPath.compile(line);
} catch (XPathExpressionException e1) {
e1.printStackTrace();
}

InputSource source = new InputSource(new StringReader(xmlString));
NodeList value = null;
try {
value = (NodeList) expr.evaluate(source, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
for(int i = 0;i < value.getLength();i++){
Node manifest = value.item(i);
Manifest1 manifest1 = new Manifest1();
XPathExpression expr_url = null;
XPathExpression expr_branch = null;
String line_url = ".//url";
String line_branch = ".//branch";
try {
expr_url = XPath.compile(line_url);
expr_branch = XPath.compile(line_branch);
} catch (XPathExpressionException e) {

e.printStackTrace();
}

String value_url = null;
String value_branch = null;

try {
value_url = (String) expr_url.evaluate(manifest, XPathConstants.STRING);
value_branch = (String) expr_branch.evaluate(manifest, XPathConstants.STRING);
} catch (XPathExpressionException e) {

e.printStackTrace();
}

manifest1.setUrl(value_url);
manifest1.setBranch(value_branch);
list.add(manifest1);
Node node = value.item(i);

}

return list;

}

从代码分析,我们先将节点全部取出,结果是一个nodelist,然后对于每一个node我们再一次取其其中的节点的值,如此便能实现我们的要求,注意在node下取其中的值的时候路径写的是全路径包括从autotest节点开始的而不是从manifest节点开始。