簡體   English   中英

使用Xpath提取屬性

[英]attribute extraction using Xpath

我有一個XML作為Java String:

String abc = "<Tags  Value = '635' Type = 'Number'/>";

我正在嘗試使用Xpath提取此類XML對象的Value

到目前為止,我一直在嘗試以下內容:

InputSource doc = new InputSource(abc);
XPath xPath =  XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("/Tags[@Value]");
System.out.println(expr.evaluate(doc));
 InputSource doc = new InputSource(abc);
 XPath xPath =  XPathFactory.newInstance().newXPath();
 XPathExpression expr = xPath.compile("/Tags/@Value");
 System.out.println(expr.evaluate(doc));

試試吧

public class Example {

    public static void main(String[] args) throws FileNotFoundException, XPathExpressionException {
        String abc = "<Tags  Value = '635' Type = 'Number'/>";
        InputSource doc = new InputSource(new StringReader(abc));
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xPath.compile("/Tags/@Value");
        System.out.println(expr.evaluate(doc)); // 635
    }

}

暫無
暫無

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

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