public static Node getFirstNode ( Document doc , String xPathExpression ) { try { XPathFactory xPathfactory = XPathFactory . newInstance ( ) ; XPath xpath = xPathfactory . newXPath ( ) ; XPathExpression expr ; expr = xpath . compile ( xPathExpression ) ; NodeList nl = ( NodeList ) expr . evaluate ( doc , XPathConstants . NODESET ) ; return nl . item ( 0 ) ; } catch ( XPathExpressionException e ) { e . printStackTrace ( ) ; } return null ; }
Get first node on a Document doc give a xpath Expression