use of javax.xml.xpath.XPathExpressionException 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.XPathExpressionException 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.XPathExpressionException in project openhab1-addons by openhab.
the class IhcResourceInteractionService method resourceQuery.
/**
* Query resource value from controller.
*
*
* @param resoureId
* Resource Identifier.
* @return Resource value.
*/
public WSResourceValue resourceQuery(int resoureId) throws IhcExecption {
final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soapenv:Body>" + " <ns1:getRuntimeValue1 xmlns:ns1=\"utcs\">%s</ns1:getRuntimeValue1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
String query = String.format(soapQuery, String.valueOf(resoureId));
openConnection(url);
String response = sendQuery(query, timeout);
closeConnection();
NodeList nodeList;
try {
nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getRuntimeValue2");
if (nodeList.getLength() == 1) {
WSResourceValue val = parseResourceValue(nodeList.item(0), 2);
if (val.getResourceID() == resoureId) {
return val;
} else {
throw new IhcExecption("No resource id found");
}
} else {
throw new IhcExecption("No resource value found");
}
} catch (XPathExpressionException e) {
throw new IhcExecption(e);
} catch (UnsupportedEncodingException e) {
throw new IhcExecption(e);
}
}
use of javax.xml.xpath.XPathExpressionException 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.XPathExpressionException in project robovm by robovm.
the class XPathImpl method compile.
/**
* <p>Compile an XPath expression for later evaluation.</p>
*
* <p>If <code>expression</code> contains any {@link XPathFunction}s,
* they must be available via the {@link XPathFunctionResolver}.
* An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
* cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
*
* <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
*
* @param expression The XPath expression.
*
* @return Compiled XPath expression.
* @throws XPathExpressionException If <code>expression</code> cannot be compiled.
* @throws NullPointerException If <code>expression</code> is <code>null</code>.
*/
public XPathExpression compile(String expression) throws XPathExpressionException {
if (expression == null) {
String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "XPath expression" });
throw new NullPointerException(fmsg);
}
try {
org.apache.xpath.XPath xpath = new XPath(expression, null, prefixResolver, org.apache.xpath.XPath.SELECT);
// Can have errorListener
XPathExpressionImpl ximpl = new XPathExpressionImpl(xpath, prefixResolver, functionResolver, variableResolver, featureSecureProcessing);
return ximpl;
} catch (javax.xml.transform.TransformerException te) {
throw new XPathExpressionException(te);
}
}
Aggregations