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