Search in sources :

Example 6 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project nutz by nutzam.

the class Xmls method getEles.

/**
     * 从一个 XML 元素开始,根据一条 XPath 获取一组元素
     * 
     * @param ele
     *            XML 元素
     * @param xpath
     *            要获取的元素的 XPath
     * @return 元素列表
     */
public static List<Element> getEles(Element ele, String xpath) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xp = factory.newXPath();
    try {
        XPathExpression expression = xp.compile(xpath);
        NodeList nodes = (NodeList) expression.evaluate(ele, XPathConstants.NODESET);
        List<Element> list = new ArrayList<Element>();
        int len = nodes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                list.add((Element) node);
            }
        }
        return list;
    } catch (XPathExpressionException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList)

Example 7 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project nutz by nutzam.

the class Xmls method getEle.

/**
     * 从一个 XML 元素开始,根据一条 XPath 获取一个元素
     * 
     * @param ele
     *            XML 元素
     * @param xpath
     *            要获取的元素的 XPath
     * @return 元素,null 表示不存在
     */
public static Element getEle(Element ele, String xpath) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xp = factory.newXPath();
    try {
        XPathExpression expression = xp.compile(xpath);
        return (Element) expression.evaluate(ele, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element)

Example 8 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project openhab1-addons by openhab.

the class IhcResourceInteractionService method resourceQuery.

/**
     * Query resource value from controller.
     * 
     * 
     * @param resoureId
     *            Resource Identifier.
     * @return Resource value.
     */
public WSResourceValue resourceQuery(int resoureId) throws IhcExecption {
    final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soapenv:Body>" + " <ns1:getRuntimeValue1 xmlns:ns1=\"utcs\">%s</ns1:getRuntimeValue1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
    String query = String.format(soapQuery, String.valueOf(resoureId));
    openConnection(url);
    String response = sendQuery(query, timeout);
    closeConnection();
    NodeList nodeList;
    try {
        nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getRuntimeValue2");
        if (nodeList.getLength() == 1) {
            WSResourceValue val = parseResourceValue(nodeList.item(0), 2);
            if (val.getResourceID() == resoureId) {
                return val;
            } else {
                throw new IhcExecption("No resource id found");
            }
        } else {
            throw new IhcExecption("No resource value found");
        }
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
}
Also used : WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project openhab1-addons by openhab.

the class WSBaseDataType method parseValue.

public static String parseValue(String xml, String xpathExpression) throws IhcExecption {
    InputStream is;
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(is);
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new NullPointerException("Null prefix");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            } else if ("ns2".equals(prefix)) {
                return "utcs.values";
            }
            return null;
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    try {
        return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) ByteArrayInputStream(java.io.ByteArrayInputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 10 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project robovm by robovm.

the class XPathImpl method compile.

/**
     * <p>Compile an XPath expression for later evaluation.</p>
     *
     * <p>If <code>expression</code> contains any {@link XPathFunction}s,
     * they must be available via the {@link XPathFunctionResolver}.
     * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
     * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
     * 
     * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
     *
     * @param expression The XPath expression.
     *
     * @return Compiled XPath expression.

     * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
     * @throws NullPointerException If <code>expression</code> is <code>null</code>.
     */
public XPathExpression compile(String expression) throws XPathExpressionException {
    if (expression == null) {
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "XPath expression" });
        throw new NullPointerException(fmsg);
    }
    try {
        org.apache.xpath.XPath xpath = new XPath(expression, null, prefixResolver, org.apache.xpath.XPath.SELECT);
        // Can have errorListener
        XPathExpressionImpl ximpl = new XPathExpressionImpl(xpath, prefixResolver, functionResolver, variableResolver, featureSecureProcessing);
        return ximpl;
    } catch (javax.xml.transform.TransformerException te) {
        throw new XPathExpressionException(te);
    }
}
Also used : org.apache.xpath(org.apache.xpath) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)139 NodeList (org.w3c.dom.NodeList)65 XPath (javax.xml.xpath.XPath)64 Document (org.w3c.dom.Document)46 Node (org.w3c.dom.Node)46 IOException (java.io.IOException)42 XPathExpression (javax.xml.xpath.XPathExpression)38 SAXException (org.xml.sax.SAXException)27 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)25 XPathFactory (javax.xml.xpath.XPathFactory)23 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 InputSource (org.xml.sax.InputSource)17 Element (org.w3c.dom.Element)16 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)11