List of usage examples for javax.xml.xpath XPathConstants NODESET
QName NODESET
To view the source code for javax.xml.xpath XPathConstants NODESET.
Click Source Link
The XPath 1.0 NodeSet data type.
Maps to Java org.w3c.dom.NodeList .
From source file:Main.java
/** * Returns the DOM nodes associated to a given xpath expression. * /*from ww w.j a va 2 s . c om*/ * @param node The root DOM node * @param xpath The xpath expression * @return The extracted DOM nodes * @throws XPathExpressionException If the given xpath expression is invalid */ public static NodeList getNodes(Node node, String xpath) throws XPathExpressionException { return (NodeList) XPATH_FACTORY.newXPath().evaluate(xpath, node, XPathConstants.NODESET); }
From source file:Main.java
public static String[] xpathOnString(String q, String stringdoc) { try {//from ww w .j a va2 s . c om 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
private static void removeEmptyTextNodes(final Document document) throws XPathExpressionException { final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { final Node node = nodeList.item(i); node.getParentNode().removeChild(node); }/* w w w . j av a2s . c om*/ }
From source file:Main.java
/** * Select node list what matches given xpath query * * @param doc xml document/* w w w . j av a 2s . c om*/ * @param expression xpath query * @return nodes which confirms given xpath query. * @throws XPathExpressionException in case of any errors. */ public static NodeList query(final Document doc, final String expression) throws XPathExpressionException { final XPath xpath = XPathFactory.newInstance().newXPath(); return (NodeList) xpath.evaluate(expression, doc.getDocumentElement(), XPathConstants.NODESET); }
From source file:Main.java
/** * Get xml nodes by xpath expression// w w w. j a v a2s .c o m * * @param doc * @param expr * @return * @throws Exception */ public static NodeList getNodesByXPath(Document doc, XPathExpression expr) throws Exception { return (NodeList) expr.evaluate(doc, XPathConstants.NODESET); }
From source file:Main.java
public static NodeList getNodeListViaXPath(String xpathExpr, String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(xml); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(xpathExpr); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); return nl;// w ww.j a v a 2 s.c om }
From source file:Main.java
public static boolean nodeExist(InputStream is, String xpathStr) throws SAXException, IOException, XPathExpressionException { boolean result = false; Document doc = null;//from w ww . j ava2 s . co m if (is == null) return result; doc = dombuilder.parse(is); Object obj = xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODESET); if (obj != null) { NodeList nodes = (NodeList) obj; if (nodes.getLength() > 0) { result = true; } } return result; }
From source file:Main.java
/** * Performs a xpath query on a document and returns the matching nodelist. * * @param doc//w w w . j ava2 s. c o m * @param xpathQuery * @return nodelist of matches * @throws Exception */ public static NodeList query(Document doc, String xpathQuery) throws Exception { NodeList result = null; // prepare XPath XPath xpath = XPathFactory.newInstance().newXPath(); try { // query xml result = (NodeList) xpath.evaluate(xpathQuery, doc, XPathConstants.NODESET); } catch (XPathExpressionException xpx) { throw new Exception("Error evaluating XPath: " + xpx.getMessage(), xpx); } return result; }
From source file:Main.java
public static HashMap xmltoHashMap222(String xmlFile, String xpath) { try {//from ww w . j av a 2 s . c o m XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); // XPathExpression xPathExpression = // xPath.compile("/history"); File xmlDocument = new File(xmlFile); InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)); // String root = xPath.evaluate("/", inputSource); NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET); HashMap hashmap = new HashMap(); for (int x = 0; x < nodes.getLength(); x++) { hashmap.put(nodes.item(x).getNodeName(), nodes.item(x).getTextContent()); } return hashmap; } catch (Exception ex) { return null; } }
From source file:Main.java
public static Vector<String> readXMLNode222(String xmlFile, String xpath) { try {/*w w w. j a v a 2 s. c o m*/ XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); // XPathExpression xPathExpression = // xPath.compile("/history"); File xmlDocument = new File(xmlFile); InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)); // String root = xPath.evaluate("/", inputSource); NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET); Vector<String> vector = new Vector<String>(); for (int x = 0; x < nodes.getLength(); x++) { vector.add(nodes.item(x).getTextContent()); } return vector; } catch (Exception ex) { ex.printStackTrace(); return new Vector(); } }