Betafish
Betafish

Reputation: 1262

attribute extraction using Xpath

I have an XML as Java String:

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

I am trying to extract the Value of such an XML object using Xpath

What I tried until now is something like this:

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

Upvotes: 0

Views: 54

Answers (2)

rathna
rathna

Reputation: 1083

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

Upvotes: 1

Roma Khomyshyn
Roma Khomyshyn

Reputation: 1152

try it

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
    }

}

Upvotes: 1

Related Questions