Recuperar un boolean a partir de una expresión XPath

1874 vistas

Veamos un método que permite recuperar un boolean (java.lang.Boolean) con XPath.



java
  1. public static Boolean evaluador(InputStream stream, String expression){
  2.   Boolean b = null;
  3.   try{
  4.       // creación del source
  5.       InputSource source = new InputSource(stream);
  6.  
  7.       // creación del XPath
  8.       XPathFactory factory = XPathFactory.newInstance();
  9.       XPath xpath = factory.newXPath();
  10.  
  11.       // evaluar la expresión XPath
  12.       XPathExpression exp = xpath.compile(expression);
  13.       b = (Boolean)exp.evaluate(source,XPathConstants.BOOLEAN);
  14.   }catch(XPathExpressionException xpee){
  15.       xpee.printStackTrace();
  16.   }
  17.   return b;
  18. }