使用XPath属性提取



我有一个xml作为java字符串:

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));

尝试

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
    }
}
 InputSource doc = new InputSource(abc);
 XPath xPath =  XPathFactory.newInstance().newXPath();
 XPathExpression expr = xPath.compile("/Tags/@Value");
 System.out.println(expr.evaluate(doc));

最新更新