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 XPathExpression compileXPathExpression(String xPathExpression) { final XPathFactory factory = XPathFactory.newInstance(); final XPath xpath = factory.newXPath(); try {/*from w w w.j a va 2s. c om*/ return xpath.compile(xPathExpression); } catch (XPathExpressionException e) { throw new RuntimeException("Internal error, invalid XPATH expression " + xPathExpression, e); } }
From source file:Main.java
/** * Evaluates a boolean xpath. Expensive as it creates a new xpath for every evaluation. */// w w w .java2s . c o m public static boolean evaluateBoolean(String xpathExpression, Document doc) throws XPathExpressionException { javax.xml.xpath.XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Boolean result = (Boolean) xpath.evaluate(xpathExpression, doc, XPathConstants.BOOLEAN); return result; }
From source file:Main.java
public static Node selectSingleNode(String express, Object source) { Node result = null;//from w w w . j a v a 2s. c o m XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { result = (Node) xpath.evaluate(express, source, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static NodeList selectNodes(String express, Object source) { NodeList result = null;/* w w w . ja v a 2s . co m*/ XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static HashMap xmltoHashMap222(String xmlFile, String xpath) { try {/*from ww w . ja 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); 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
private static Document getDoc(String filePath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w. j a v a2 s .com DocumentBuilder builder = null; XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); builder = factory.newDocumentBuilder(); Document doc = builder.parse(filePath); return doc; }
From source file:Main.java
public static Vector<String> readXMLNode222(String xmlFile, String xpath) { try {/*from ww w .j a va2 s . com*/ 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(); } }
From source file:Main.java
/** * Evaluate an XPath against a Document, returning a String. * /*from w ww .j av a 2s. c o m*/ * @param doc Document * @param xp XPath to evaluate against Document * @return String found at path or null */ public static String xpathOrNull(Document doc, String xp) { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); try { XPathExpression expr = xpath.compile(xp); return expr.evaluate(doc); } catch (XPathExpressionException e) { return null; } }
From source file:Main.java
private static XPathExpression evaluateThePath(String xPath) throws XPathExpressionException { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); return xpath.compile(xPath + "/text()");//small hack to get to the actual value as this is what we actually need and is mostly used }
From source file:Main.java
public static String getValueAsString(String filePath, String value) throws ParserConfigurationException, SAXException, IOException { String id = null;/* w ww .j a v a2 s. co m*/ XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { XPathExpression expr = xpath.compile(value); id = (String) expr.evaluate(getDoc(filePath), XPathConstants.STRING); } catch (XPathExpressionException e) { e.printStackTrace(); } return id; }