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