List of usage examples for javax.xml.xpath XPathFactory newXPath
public abstract XPath newXPath();
Return a new XPath
using the underlying object model determined when the XPathFactory was instantiated.
From source file:Main.java
public static NodeList getValueAsNodes(String filePath, String value) throws ParserConfigurationException, SAXException, IOException { NodeList nodeList = null;/* ww w .j a v a 2 s.c o m*/ XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { XPathExpression expr = xpath.compile(value); nodeList = (NodeList) expr.evaluate(getDoc(filePath), XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return nodeList; }
From source file:Main.java
public static Object execXpathGetNodeList(String srcXmlString, String xPath) { Object result = null;/*from www . j av a 2 s . c o m*/ try { Document doc = stringToDoc(srcXmlString); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); result = expr.evaluate(doc, XPathConstants.NODESET); } catch (Exception ex) { logger.error(ex); } return result; }
From source file:Main.java
private static Node getNodeObject(String xpathString, Document doc) throws XPathExpressionException { // Create a xptah object //logger.debug("Getting the node by using xpath " + xpathString); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expr = xpath.compile(xpathString); Node node = (Node) expr.evaluate(doc, XPathConstants.NODE); //logger.debug("Returning the Node object from xml file"); return node;/*from w w w . ja va 2s. c o m*/ }
From source file:Main.java
public static String[] xpathOnString(String q, String stringdoc) { try {// ww w . j a v a 2s . c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(q); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String res[] = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { // System.out.println(nodes.item(i).toString()); res[i] = nodes.item(i).getNodeValue(); } return res; } catch (Exception e) { System.out.println("XPathUtils.xpathOnString:caught:" + e); return null; } }
From source file:Main.java
/** * Execute an xpath query on a specific node. * * The type parameter specifies the expected type of the result of the * query. Typical types are:/* w w w. ja va2s . c om*/ * * XPathContants.NUMBER: A java Double that corresponds to a numeric * literal in the XML document * XPathConstants.STRING: A java String that corresponds to a text literal * in the XML document * XPathConstants.NODESET: A NodeList that contains a list of nodes * in the XML document * XPathConstants.NODE: A particular Node inside the XML document * * @param doc The node on which the xpath expression is to be executed * @param xpath_expr The XPath expression to be executed * @param type One of the XPathConstants that specified the expected output. * @return The result of the XPath query on the node, of the corresponding * type */ @SuppressWarnings("unchecked") public static <T> T xpath(Node doc, String xpath_expr, QName type) { XPathFactory xpathfactory = XPathFactory.newInstance(); XPath xpath = xpathfactory.newXPath(); XPathExpression expr; try { expr = xpath.compile(xpath_expr); return (T) expr.evaluate(doc, type); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String getNodeTextByXPath(Document doc, String xpath) { try {/*from w ww. j av a2s .c o m*/ XPathFactory factory = XPathFactory.newInstance(); XPath xpathEn = factory.newXPath(); return xpathEn.evaluate(xpath, doc); } catch (Exception ex) { ex.printStackTrace(); return ""; } }
From source file:Main.java
public static XPath namespaceAwareXpath(final String prefix, final String nsURI) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NamespaceContext ctx = new NamespaceContext() { @Override/*from w ww .j a v a2 s . c om*/ public String getNamespaceURI(String aPrefix) { if (aPrefix.equals(prefix)) return nsURI; else return null; } @Override public Iterator getPrefixes(String val) { throw new UnsupportedOperationException(); } @Override public String getPrefix(String uri) { throw new UnsupportedOperationException(); } }; xpath.setNamespaceContext(ctx); return xpath; }
From source file:Main.java
public static String extractValue(String xml, String xpathExpression) { String actual;// ww w . j a v a2 s . c om try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); byte[] bytes = xml.getBytes("UTF-8"); InputStream inputStream = new ByteArrayInputStream(bytes); Document doc = docBuilder.parse(inputStream); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString(); } catch (Exception e) { throw new RuntimeException(e); } return actual; }
From source file:Main.java
/** * Evaluates the XPath expression against the <code>xml</code> and returns the selected value. * /*www.ja v a 2 s. c o m*/ * @param expression Expression to evaluate. * @param xml The xml to query. * @return The selected value. * @throws XPathExpressionException If an error occurs evaluating the expression. */ public static String getValue(final String expression, final String xml) throws XPathExpressionException { final InputSource source = new InputSource(new StringReader(xml)); final XPathFactory factory = XPathFactory.newInstance(); final XPath xpath = factory.newXPath(); return xpath.evaluate(expression, source); }
From source file:Main.java
/** * Use an Xpath expression on the given node or document. * * @param _xpathExpression xpath expression string * @param _xmlDocumentOrNode a {@link Document} or {@link Node} object * @return NodeList never null//from w w w . ja v a2 s . c o m * @throws IOException on error */ public static NodeList applyXpathExpressionToDocument(String _xpathExpression, Node _xmlDocumentOrNode) throws IOException { XPathFactory xfactory = XPathFactory.newInstance(); XPath xpath = xfactory.newXPath(); XPathExpression expr = null; try { expr = xpath.compile(_xpathExpression); } catch (XPathExpressionException _ex) { throw new IOException(_ex); } Object result = null; try { result = expr.evaluate(_xmlDocumentOrNode, XPathConstants.NODESET); } catch (Exception _ex) { throw new IOException(_ex); } return (NodeList) result; }