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(final String uri) throws XPathFactoryConfigurationException 

Source Link

Document

Get a new XPathFactory instance using the specified object model.

To find a XPathFactory object, this method looks the following places in the following order where "the class loader" refers to the context class loader:

  1. If the system property #DEFAULT_PROPERTY_NAME + ":uri" is present, where uri is the parameter to this method, then its value is read as a class name.

    Usage

    From source file:Main.java

    public static Object getNodesListXpath(String XpathS, Node node, String nsuri, String pre, QName returnType)
            throws Exception {
        Object matches = null;/*  ww w. j a  va2 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 XPathFactory getXPathFactory() throws XPathFactoryConfigurationException {
        if (_xPathFactory == null) {
            String magicValue = System
                    .getProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom");
            System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom",
                    "net.sf.saxon.xpath.XPathFactoryImpl");
            //          System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom","org.apache.xpath.jaxp.XPathFactoryImpl");
            //          System.setProperty("jaxp.debug","yes");
            _xPathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
            if (magicValue == null)
                System.clearProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom");
            else/*  w ww .  j  av  a  2 s .  c  om*/
                System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom", magicValue);
        }
        return _xPathFactory;
    }
    

    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 .  java  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

    /**
     * //  w w w.java2  s . c o  m
     * @param xPathS
     * @param node
     * @param nsuri
     * @param pre
     * @param returnType
     * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET,
     *         .NUMBER, .STRING
     * @throws Exception
     */
    public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri,
            final String pre, final QName returnType) throws Exception {
        Object matches = null;
        System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY);
    
        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:com.servicelibre.jxsl.scenario.test.xspec.XspecTestScenarioRunner.java

    private void init() {
    
        XPath xpath = null;//from ww  w . ja  va2s.  c om
        try {
            xpath = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON).newXPath();
        } catch (XPathFactoryConfigurationException e1) {
            logger.error("Error while creating XPathFactory", e1);
            return;
        }
    
        SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
        namespaceContext.bindNamespaceUri("x", "http://www.jenitennison.com/xslt/xspec");
        xpath.setNamespaceContext(namespaceContext);
    
        try {
    
            successXpath = xpath.compile("count(//x:test[@successful ='false'] ) = 0");
            testFailedCount = xpath.compile("count(//x:test[@successful ='false'] )");
            testCount = xpath.compile("count(//x:test)");
        } catch (XPathExpressionException e) {
            logger.error("Error while initializing {}.", this.getClass().getName(), e);
        }
    
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
    
        try {
            xmlBuilder = docFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            logger.error("Error while configuring XML parser", e);
        }
    
    }