Search in sources :

Example 11 with XPathExpression

use of javax.xml.xpath.XPathExpression 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 XPathExpression

use of javax.xml.xpath.XPathExpression 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 13 with XPathExpression

use of javax.xml.xpath.XPathExpression 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 14 with XPathExpression

use of javax.xml.xpath.XPathExpression 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 15 with XPathExpression

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

the class XAdESSignaturePropertiesTest method prefixAndNamespace.

@Test
public void prefixAndNamespace() throws Exception {
    XmlSignerEndpoint endpoint = getSignerEndpoint();
    XAdESSignatureProperties props = (XAdESSignatureProperties) endpoint.getProperties();
    props.setPrefix("p");
    props.setNamespace(XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_1_1);
    props.setCommitmentTypeIdDescription(null);
    props.setCommitmentTypeIdDocumentationReferences(Collections.<String>emptyList());
    props.setCommitmentTypeIdQualifier(null);
    props.setDataObjectFormatIdentifierDescription(null);
    props.setDataObjectFormatIdentifierDocumentationReferences(Collections.<String>emptyList());
    props.setDataObjectFormatIdentifierQualifier(null);
    props.setSigPolicyIdDescription(null);
    props.setSigPolicyIdDocumentationReferences(Collections.<String>emptyList());
    props.setSigPolicyIdQualifier(null);
    // the following lists must be set to empty because otherwise they would contain XML fragments with a wrong namespace
    props.setSigPolicyQualifiers(Collections.<String>emptyList());
    props.setSignerClaimedRoles(Collections.<String>emptyList());
    props.setCommitmentTypeQualifiers(Collections.<String>emptyList());
    Document doc = testEnveloping();
    Map<String, String> prefix2Namespace = new TreeMap<String, String>();
    prefix2Namespace.put("ds", XMLSignature.XMLNS);
    prefix2Namespace.put("etsi", XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_1_1);
    XPathExpression expr = getXpath("/ds:Signature/ds:Object/etsi:QualifyingProperties", prefix2Namespace);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    assertNotNull(result);
    Node node = (Node) result;
    assertEquals("p", node.getPrefix());
    assertEquals(XAdESSignatureProperties.HTTP_URI_ETSI_ORG_01903_V1_1_1, node.getNamespaceURI());
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XAdESSignatureProperties(org.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties) DefaultXAdESSignatureProperties(org.apache.camel.component.xmlsecurity.api.DefaultXAdESSignatureProperties) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 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 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9