Example usage for javax.xml.xpath XPathFactory newInstance

List of usage examples for javax.xml.xpath XPathFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newInstance.

Prototype

public static XPathFactory newInstance() 

Source Link

Document

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.

Usage

From source file:Main.java

private static XPath createXpath() {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    return xpath;
}

From source file:Main.java

private static XPathExpression compile(String path) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    try {// w  ww . ja  va  2  s . c  o m
        return xpath.compile(path);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.wso2telco.identity.application.authentication.endpoint.util.ReadMobileConnectConfig.java

public static Map<String, String> query(String XpathExpression) {

    Map<String, String> ConfigfileAttributes = new Hashtable<String, String>();

    // standard for reading an XML file
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*w  w  w.ja v a 2  s .c om*/
    DocumentBuilder builder;
    Document doc = null;
    XPathExpression expr = null;
    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(CarbonUtils.getCarbonConfigDirPath() + File.separator + "mobile-connect.xml");

        // create an XPathFactory
        XPathFactory xFactory = XPathFactory.newInstance();

        // create an XPath object
        XPath xpath = xFactory.newXPath();

        // compile the XPath expression
        expr = xpath.compile("//" + XpathExpression + "/*");
        // run the query and get a nodeset
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        //        // cast the result to a DOM NodeList
        NodeList nodes = (NodeList) result;

        for (int i = 0; i < nodes.getLength(); i++) {
            ConfigfileAttributes.put(nodes.item(i).getNodeName(), nodes.item(i).getTextContent());
        }
    } catch (SAXException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage());
    } catch (XPathExpressionException e) {
        log.error(e.getMessage());
    }

    return ConfigfileAttributes;
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

public static String updateTemplateName(String nifiTemplate, String newName) throws Exception {

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Document doc = stringToDocument(nifiTemplate);

    InputSource source = new InputSource(new StringReader(nifiTemplate));

    Element element = (Element) xpath.compile("//template/name").evaluate(doc, XPathConstants.NODE);

    element.setTextContent(newName);/*from   ww  w.j  a  va  2s.c  o  m*/
    return documentToString(doc);
}

From source file:Main.java

/**
 * Evaluates the XPath expression against the <code>xml</code> and returns the selected nodes.
 * /*from www . j ava 2s . co  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:ch.entwine.weblounge.common.impl.util.xml.XPathHelper.java

/**
 * Returns the query result or <code>null</code>.
 * <p>//www  .j  av  a2s . com
 * <b>Note:</b> This signature creates a new <code>XPath</code> processor on
 * every call, which is probably fine for testing but not favorable when it
 * comes to production use, since creating an <code>XPath</code> processor is
 * resource intensive.
 * 
 * @param node
 *          the context node
 * @param xpath
 *          the xpath expression
 * 
 * @return the selected string or <code>null</code> if the query didn't yield
 *         a result
 */
public static String valueOf(Node node, String xpathExpression) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    return valueOf(node, xpathExpression, null, xpath);
}

From source file:com.orange.ngsi.model.UpdateContextModelTest.java

@Test
public void serializationXML() throws IOException, URISyntaxException, XPathExpressionException {

    String xml = xmlmapper.writeValueAsString(createUpdateContextTempSensor(0));

    String xpathExpr = "/updateContextRequest/contextElementList/contextElement[1]/entityId/id";
    XPath xPath = XPathFactory.newInstance().newXPath();
    String value = xPath.evaluate(xpathExpr, new InputSource(new StringReader(xml)));
    assertEquals("S1", value);
}

From source file:ch.entwine.weblounge.common.impl.testing.IntegrationTestParser.java

/**
 * Initializes an integration test object from the given test definition.
 * <p>// www . j av  a2  s  .  co  m
 * To speed things up, you might consider using the second signature that uses
 * an existing <code>XPath</code> instance instead of creating a new one.
 * 
 * @param config
 *          the test node
 * @throws IllegalStateException
 *           if the test cannot be parsed
 * @see #fromXml(Node, XPath)
 */
public static IntegrationTestGroup fromXml(Node config) throws IllegalStateException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Define the xml namespace
    XPathNamespaceContext nsCtx = new XPathNamespaceContext(false);
    nsCtx.defineNamespaceURI("m", TEST_XMLNS);
    xpath.setNamespaceContext(nsCtx);

    return fromXml(config, xpath);
}

From source file:cz.incad.kramerius.virtualcollections.VirtualCollectionsManager.java

public static VirtualCollection doVC(String pid, FedoraAccess fedoraAccess, ArrayList<String> languages) {
    try {//  w ww.j a  va2s . c  o  m
        String xPathStr;
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr;

        ArrayList<String> langs = new ArrayList<String>();
        if (languages == null || languages.isEmpty()) {
            String[] ls = KConfiguration.getInstance().getPropertyList("interface.languages");
            for (int i = 0; i < ls.length; i++) {
                String lang = ls[++i];
                langs.add(lang);
            }
        } else {
            langs = new ArrayList<String>(languages);
        }
        String name = "";
        boolean canLeave = true;
        fedoraAccess.getDC(pid);
        Document doc = fedoraAccess.getDC(pid);
        xPathStr = "//dc:title/text()";
        expr = xpath.compile(xPathStr);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            name = StringEscapeUtils.escapeXml(node.getNodeValue());
        }

        xPathStr = "//dc:type/text()";
        expr = xpath.compile(xPathStr);
        node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            canLeave = Boolean.parseBoolean(StringEscapeUtils.escapeXml(node.getNodeValue()));
        }
        VirtualCollection vc = new VirtualCollection(name, pid, canLeave);

        for (String lang : langs) {
            String dsName = TEXT_DS_PREFIX + lang;
            String value = IOUtils.readAsString(fedoraAccess.getDataStream(pid, dsName),
                    Charset.forName("UTF8"), true);
            vc.addDescription(lang, value);
        }
        return vc;
    } catch (Exception vcex) {
        logger.log(Level.WARNING, "Could not get virtual collection for  " + pid + ": " + vcex.toString());
        return null;
    }
}

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * //w  w  w .j av a2 s .  c o m
 * @param xml
 *            the xml
 * @param xpathExpression
 *            the xpath expression
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(final File xml, final String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    final DocumentBuilder builder = domFactory.newDocumentBuilder();
    final Document doc = builder.parse(xml);
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final XPathExpression expr = xpath.compile(xpathExpression);

    final Object result = expr.evaluate(doc, XPathConstants.NODESET);
    final NodeList nodes = (NodeList) result;
    return nodes;
}