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);
}
}
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);
}
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);
}
}
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);
}
}
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());
}
Aggregations