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

首先是我们需要查询的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节点开始。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值