簡體   English   中英

使用預編譯的xpath的條件xpath

[英]Conditional xpath using precompiled xpath

我在項目中使用XPATH,我需要有條件地遍歷節點

public static String getNodeContentForMultipleTag1(XPathExpression expr,Document doc) {
    try {

        NodeList typeResult = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < typeResult.getLength(); i++) {
            Node typeResultNode = typeResult.item(i);
            System.out.println(typeResultNode.getTextContent());
        }
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Failed parsing expression",e);
    }
    return null;
}

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    String s="<ex><DtTm><TxDtTm><Cd>ABCD</Cd><dt>1234</dt></TxDtTm><TxDtTm><Cd>XYZ</Cd><dt>891</dt></TxDtTm></DtTm></ex>";
    InputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
    DocumentBuilder db= XpathInstanceUtil.getDocumentBuilderFactory().newDocumentBuilder();
    Document doc = db.parse(inputStream);
    XPath xpath = XpathInstanceUtil.getXPathFactory().newXPath();
    XPathExpression expr = xpath.compile("/ex/DtTm/TxDtTm");
    inputStream.close();
    long st = System.currentTimeMillis();
    getNodeContentForMultipleTag1(expr, doc);
    long end = System.currentTimeMillis();
    System.out.println(end-st);

    long st1 = System.currentTimeMillis();
    getNodeContentForMultipleTag1(expr, doc);
    long end1 = System.currentTimeMillis();
    System.out.println(end1-st1);

}

如果Cd值為ABCD,我應該得到1234。 我嘗試了以下

public static String getNodeContentForMultipleTag(String expresssion,String expectedNode,String expectedExpressionTag,Document doc) {
    try {
        XPath xpath = XpathInstanceUtil.getXPathFactory().newXPath();
        NodeList typeResult = (NodeList) evaluateXPath(doc,expresssion,xpath,XPathConstants.NODESET);
        NodeList valueResult= (NodeList) evaluateXPath(doc,expectedExpressionTag,xpath,XPathConstants.NODESET);
        //NodeList typeResult = (NodeList) xpath.evaluate(expresssion,doc, XPathConstants.NODESET);
        //NodeList valueResult = (NodeList) xpath.evaluate(expectedExpressionTag,doc, XPathConstants.NODESET);
        for (int i = 0; i < typeResult.getLength(); i++) {
            Node typeResultNode = typeResult.item(i);
            typeResultNode.getParentNode().removeChild(typeResultNode);
            Node valueResultNode = valueResult.item(i);
            if(typeResultNode.getTextContent().equals(expectedNode) && valueResultNode!=null){
                valueResultNode.getParentNode().removeChild(valueResultNode);
                return  valueResultNode.getTextContent();
            }
        }
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Failed parsing expression"+expresssion,e);
    }
    return null;
}

這就是表情的樣子

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        String s="<ex><DtTm><TxDtTm><Cd>ABCD</Cd><dt>1234</dt></TxDtTm><TxDtTm><Cd>XYZ</Cd><dt>891</dt></TxDtTm></DtTm></ex>";
        InputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
        DocumentBuilder db= XpathInstanceUtil.getDocumentBuilderFactory().newDocumentBuilder();
        Document doc = db.parse(inputStream);
        String ss = getNodeContentForMultipleTag("/ex/DtTm/TxDtTm/Cd", "XYZ", "/ex/DtTm/TxDtTm/dt", doc);
        System.out.println(ss);
    }

但是它的性能很低,應該如何更改才能有效解析

這段代碼似乎完全奇怪。 為什么要用Java而不是XPath來完成所有這些工作? 為什么在搜索時修改DOM樹?

您只需要執行XPath表達式/ex/DtTm/TxDtTm[Cd='ABCD']/dt就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM