List of usage examples for javax.xml.xpath XPathFactory newInstance
public static XPathFactory newInstance()
Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.
This method is functionally equivalent to:
newInstance(DEFAULT_OBJECT_MODEL_URI)
Since the implementation for the W3C DOM is always available, this method will never fail.
From source file:Main.java
public static String queryText(Document document, String xPath) { try {//from w w w . j a v a 2 s .com XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath); return (String) expr.evaluate(document, XPathConstants.STRING); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
From source file:Main.java
/** * @return//w w w.j a v a 2 s.com */ public static XPath getHtmlXPath() { XPathFactory factory = XPathFactory.newInstance(); XPath htmlPath = factory.newXPath(); htmlPath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return "http://www.w3.org/1999/xhtml"; } public String getPrefix(String namespaceURI) { return "h"; } public Iterator<?> getPrefixes(String namespaceURI) { List<String> prefixes = new ArrayList<String>(); prefixes.add("h"); return prefixes.iterator(); } }); return htmlPath; }
From source file:Main.java
public static XPathFactory getxPathFactory() { if (xPathFactory != null) { return xPathFactory; }/*from w w w.java2 s. com*/ xPathFactory = XPathFactory.newInstance(); return xPathFactory; }
From source file:Main.java
public static Node queryNode(Document document, String xPath) { try {//from w ww . j a v a 2 s . c o m XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath); return (Node) expr.evaluate(document, XPathConstants.NODE); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
From source file:Main.java
public static Node getNoge(String path, Node node) throws RuntimeException { path = path.trim();//from w ww .j a v a 2 s . c o m try { XPath xp = XPathFactory.newInstance().newXPath(); XPathExpression expr = xp.compile(path); Node lst = (Node) expr.evaluate(node, XPathConstants.NODE); return lst; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static XPath getXPathInstance() { XPath xPath = XPathFactory.newInstance().newXPath(); return xPath; }
From source file:Main.java
/** * @return//from ww w . ja v a 2 s.co m */ public static XPath getCruxPagesXPath() { XPathFactory factory = XPathFactory.newInstance(); XPath findPath = factory.newXPath(); findPath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix.equals("c")) { return "http://www.cruxframework.org/crux"; } else if (prefix.equals("v")) { return "http://www.cruxframework.org/view"; } return ""; } public String getPrefix(String namespaceURI) { if (namespaceURI.equals("http://www.cruxframework.org/crux")) { return "c"; } else if (namespaceURI.equals("http://www.cruxframework.org/view")) { return "v"; } return ""; } public Iterator<?> getPrefixes(String namespaceURI) { List<String> prefixes = new ArrayList<String>(); prefixes.add("c"); prefixes.add("v"); return prefixes.iterator(); } }); return findPath; }
From source file:Main.java
public static NodeList queryNodeList(Document document, String xPath) { try {/*from w ww .j ava 2 s . com*/ XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath); return (NodeList) expr.evaluate(document, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new IllegalArgumentException(e); } }
From source file:Main.java
public static Node selectSingleNode(final Node sourceNode, final String xPathExpression) { Node result;// w ww . java2s. c om XPathFactory factory = XPathFactory.newInstance(); // http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html XPath xPathParser = factory.newXPath(); try { result = (Node) xPathParser.evaluate(xPathExpression, sourceNode, XPathConstants.NODE); } catch (XPathExpressionException e) { result = null; } return result; }
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {//from w ww. ja v a2 s . c o m // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }