/** * Performs an XPATH lookup in an xml document whose filename is specified by the first parameter * (assumed to be in the auxiliary subdirectory) The XPATH expression is supplied as the second * string parameter The return value is of type string e.g. this is currently used primarily for * looking up the Company Prefix Index for encoding a GS1 Company Prefix into a 64-bit EPC tag */ private String xpathlookup(String xml, String expression) { try { // Parse the XML as a W3C document. DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File(xmldir + File.separator + "auxiliary" + File.separator + xml)); XPath xpath = XPathFactory.newInstance().newXPath(); String rv = (String) xpath.evaluate(expression, document, XPathConstants.STRING); return rv; } catch (ParserConfigurationException e) { System.err.println("ParserConfigurationException caught..."); e.printStackTrace(); return null; } catch (XPathExpressionException e) { System.err.println("XPathExpressionException caught..."); e.printStackTrace(); return null; } catch (SAXException e) { System.err.println("SAXException caught..."); e.printStackTrace(); return null; } catch (IOException e) { System.err.println("IOException caught..."); e.printStackTrace(); return null; } }
public double measureMetric(Document document) { double count = 0; XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { XPathExpression expression = xpath.compile(XPATH_QUERY); count = (Double) expression.evaluate(document, XPathConstants.NUMBER); } catch (XPathExpressionException e) { e.printStackTrace(); } return count; // Para retornar so 0 ou 1 usar o seguinte: // return (count > 0.0) ? 1.0 : 0.0; }