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 Map<String, String> readXmlToMap(String filename, String xpathExp, String arrtibute) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { //to-do something Map<String, String> dataMap = new HashMap<String, String>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = dbf.newDocumentBuilder(); File file = new File(filename); if (file.exists()) { Document doc = db.parse(file); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); NodeList varList = (NodeList) xpath.evaluate(xpathExp, doc, XPathConstants.NODESET); for (int i = 0; i < varList.getLength(); i++) { dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(), varList.item(i).getTextContent()); }/*from w w w . j a va2 s. c o m*/ } return dataMap; }
From source file:Main.java
public static XPathExpression buildXPath(String path, Map<String, String> map) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); if (map != null) xpath.setNamespaceContext(new NamespaceContext() { public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); }//from ww w .j ava 2s. co m public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException(); } public String getNamespaceURI(String prefix) { Objects.requireNonNull(prefix); if (map.containsKey(prefix)) return map.get(prefix); return XMLConstants.NULL_NS_URI; } }); try { return xpath.compile(path); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Object execXpathGetNode(String srcXmlString, String xPath) { Object result = null;/* w w w. ja va 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.NODE); } catch (Exception ex) { logger.error(ex); } return result; }
From source file:Main.java
/** * Create a map based on a xml file and a xpath expression and an attribute. * Treat attribute's value as map's key, treat xpath expression represented node's text content as map's value. * //w w w. j a v a2 s . c om * @param xmlFile handled xml file * @param xpathExp xpath express, such as "//var" * @param arrtibute attribute, such as "name" * @return created Map object * @throws Exception */ public static Map<String, String> readXmlToMap(String xmlFile, String xpathExp, String arrtibute) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document xmlDoc = null; try { xmlDoc = db.parse(new File(xmlFile)); } catch (SAXException e) { if ("Premature end of file.".equals(e.getMessage())) System.out.println("Maybe your local data file has no content."); //e.printStackTrace(); return new HashMap<String, String>(); } XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); NodeList varList = (NodeList) xpath.evaluate(xpathExp, xmlDoc, XPathConstants.NODESET); Map<String, String> dataMap = new HashMap<String, String>(); for (int i = 0; i < varList.getLength(); i++) dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(), varList.item(i).getTextContent()); return dataMap; }
From source file:Main.java
public static Object getNodesListXpath(String XpathS, Node node, String nsuri, String pre, QName returnType) throws Exception { Object matches = null;//from ww w. j a v a 2 s . co m // TODO move this to a generic start up method System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XpathFactory); XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpath = xpathFactory.newXPath(); XPathExpression xpe = xpath.compile(XpathS); matches = xpe.evaluate(node, returnType); return matches; }
From source file:Main.java
public static void deepCopyDocument(Document ttml, File target) throws IOException { try {/* w w w . j a v a 2s.c om*/ XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("//*/@backgroundImage"); NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node backgroundImage = nl.item(i); URI backgroundImageUri = URI.create(backgroundImage.getNodeValue()); if (!backgroundImageUri.isAbsolute()) { copyLarge(new URI(ttml.getDocumentURI()).resolve(backgroundImageUri).toURL().openStream(), new File(target.toURI().resolve(backgroundImageUri).toURL().getFile())); } } copyLarge(new URI(ttml.getDocumentURI()).toURL().openStream(), target); } catch (XPathExpressionException e) { throw new IOException(e); } catch (URISyntaxException e) { throw new IOException(e); } }
From source file:Main.java
public static Object getNodesListXpath(String XpathS, Node node, String nsuri, String pre, QName returnType) throws Exception { Object matches = null;//from w ww .ja v a 2 s. c om // TODO move this to a generic start up method //System.setProperty("javax.xml.xpath.XPathFactory:"+ XPathConstants.DOM_OBJECT_MODEL, XpathFactory); XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpath = xpathFactory.newXPath(); XPathExpression xpe = xpath.compile(XpathS); matches = xpe.evaluate(node, returnType); return matches; }
From source file:Main.java
private static XPathExpression compile(String path) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); try {//from w w w.jav a2s.c o m return xpath.compile(path); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Evaluates the XPath expression against the <code>xml</code> and returns the selected nodes. * //from www .java2s. c o m * @param expression Expression to evaluate. * @param xml The xml to query. * @return The selected nodes. * @throws XPathExpressionException If an error occurs evaluating the expression. */ public static NodeList getNodes(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 (NodeList) xpath.evaluate(expression, source, XPathConstants.NODESET); }
From source file:com.thoughtworks.go.util.XpathUtils.java
public static boolean nodeExists(InputSource inputSource, String xpath) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPathExpression expression = factory.newXPath().compile(xpath); Boolean b = (Boolean) expression.evaluate(inputSource, XPathConstants.BOOLEAN); return b != null && b; }