Search in sources :

Example 11 with XPathFactory

use of javax.xml.xpath.XPathFactory 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 12 with XPathFactory

use of javax.xml.xpath.XPathFactory in project processing by processing.

the class XML method trim.

public void trim() {
    try {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
        NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(node, XPathConstants.NODESET);
        // Remove each empty text node from document.
        for (int i = 0; i < emptyTextNodes.getLength(); i++) {
            Node emptyTextNode = emptyTextNodes.item(i);
            emptyTextNode.getParentNode().removeChild(emptyTextNode);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory)

Example 13 with XPathFactory

use of javax.xml.xpath.XPathFactory in project spring-boot by spring-projects.

the class Versions method evaluateExpression.

private static String evaluateExpression(String expression) {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr = xpath.compile(expression);
        String version = expr.evaluate(new InputSource(new FileReader("target/dependencies-pom.xml")));
        return version;
    } catch (Exception ex) {
        throw new IllegalStateException("Failed to evaluate expression", ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 14 with XPathFactory

use of javax.xml.xpath.XPathFactory in project camel by apache.

the class XPathTest method testXPathFunctionTokenizeUsingSaxonXPathFactory.

@Test
public void testXPathFunctionTokenizeUsingSaxonXPathFactory() throws Exception {
    // START SNIPPET: e1
    // create a Saxon factory
    XPathFactory fac = new net.sf.saxon.xpath.XPathFactoryImpl();
    // create a builder to evaluate the xpath using the saxon factory
    XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").factory(fac);
    // evaluate as a String result
    String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
    assertEquals("def", result);
// END SNIPPET: e1
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Example 15 with XPathFactory

use of javax.xml.xpath.XPathFactory in project camel by apache.

the class XPathTest method testXPathUsingSaxon.

@Test
public void testXPathUsingSaxon() throws Exception {
    XPathFactory fac = new XPathFactoryImpl();
    XPathBuilder builder = XPathBuilder.xpath("foo/bar").factory(fac);
    // will evaluate as XPathConstants.NODESET and have Camel convert that to String
    // this should return the String incl. xml tags
    String name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>", String.class);
    assertEquals("<bar id=\"1\">cheese</bar>", name);
    // will evaluate using XPathConstants.STRING which just return the text content (eg like text())
    name = builder.evaluate(context, "<foo><bar id=\"1\">cheese</bar></foo>");
    assertEquals("cheese", name);
}
Also used : XPathFactoryImpl(net.sf.saxon.xpath.XPathFactoryImpl) XPathFactory(javax.xml.xpath.XPathFactory) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) Test(org.junit.Test)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7