Search in sources :

Example 16 with XPath

use of javax.xml.xpath.XPath 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 17 with XPath

use of javax.xml.xpath.XPath 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 18 with XPath

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

the class IhcResourceInteractionService method getValue.

private String getValue(Node n, String expr) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    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 "utcs.values";
        // return null;
        }

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

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    XPathExpression pathExpr = xpath.compile(expr);
    return (String) pathExpr.evaluate(n, XPathConstants.STRING);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 19 with XPath

use of javax.xml.xpath.XPath 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 20 with XPath

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

the class JaxenXPathTestSuite method contextToTestSuite.

/**
     * Populates the test suite with tests from the given XML context element.
     */
private static void contextToTestSuite(TestSuite suite, String url, InputSource inputSource, Element element) {
    /*
         * Each context element has this structure:
         *
         * <context select="...">
         *   <test .../>
         *   <test .../>
         *   <test .../>
         *   <valueOf .../>
         *   <valueOf .../>
         *   <valueOf .../>
         * </context>
         */
    String select = element.getAttribute("select");
    Context context = new Context(inputSource, url, select);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setXPathVariableResolver(new ElementVariableResolver(element));
    for (Element test : elementsOf(element.getChildNodes())) {
        if (test.getTagName().equals("test")) {
            suite.addTest(createFromTest(xpath, context, test));
        } else if (test.getTagName().equals("valueOf")) {
            suite.addTest(createFromValueOf(xpath, context, test));
        } else {
            throw new UnsupportedOperationException("Unsupported test: " + context);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) Element(org.w3c.dom.Element)

Aggregations

XPath (javax.xml.xpath.XPath)197 Document (org.w3c.dom.Document)107 NodeList (org.w3c.dom.NodeList)99 XPathExpressionException (javax.xml.xpath.XPathExpressionException)70 Node (org.w3c.dom.Node)70 XPathExpression (javax.xml.xpath.XPathExpression)69 XPathFactory (javax.xml.xpath.XPathFactory)59 DocumentBuilder (javax.xml.parsers.DocumentBuilder)45 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 IOException (java.io.IOException)32 Element (org.w3c.dom.Element)32 Test (org.junit.Test)25 InputSource (org.xml.sax.InputSource)23 SAXException (org.xml.sax.SAXException)21 File (java.io.File)19 ArrayList (java.util.ArrayList)19 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 HashMap (java.util.HashMap)16 InputStream (java.io.InputStream)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11