本文整理汇总了Java中javax.xml.xpath.XPath.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Java XPath.evaluate方法的具体用法?Java XPath.evaluate怎么用?Java XPath.evaluate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.xpath.XPath
的用法示例。
在下文中一共展示了XPath.evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getVersion
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* Returns the version
*
* @return String
*/
public static String getVersion() {
String jarImplementation = VISNode.class.getPackage().getImplementationVersion();
if (jarImplementation != null) {
return jarImplementation;
}
String file = VISNode.class.getResource(".").getFile();
String pack = VISNode.class.getPackage().getName().replace(".", "/") + '/';
File pomXml = new File(file.replace("target/classes/" + pack, "pom.xml"));
if (pomXml.exists()) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new FileReader(pomXml)));
XPath xPath = XPathFactory.newInstance().newXPath();
return (String) xPath.evaluate("/project/version", document, XPathConstants.STRING);
} catch (IOException | ParserConfigurationException | XPathExpressionException | SAXException e) { }
}
return "Unknown version";
}
示例2: findElementValues
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private List<ElementValue> findElementValues(ElementValue pev, String path) {
List<ElementValue> result = new ArrayList<>();
XPath xpath = XPathFactory.newInstance().newXPath();
try {
NodeList list = (NodeList) xpath.evaluate(path, pev.getElement(), XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
result.add(new ElementValue((Element) list.item(i), Type.OK));
}
} catch (XPathExpressionException ex) {
}
return result;
}
示例3: eval
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public boolean eval() throws BuildException {
if (nullOrEmpty(fileName)) {
throw new BuildException("No file set");
}
File file = new File(fileName);
if (!file.exists() || file.isDirectory()) {
throw new BuildException(
"The specified file does not exist or is a directory");
}
if (nullOrEmpty(path)) {
throw new BuildException("No XPath expression set");
}
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource inputSource = new InputSource(fileName);
Boolean result = Boolean.FALSE;
try {
result = (Boolean) xpath.evaluate(path, inputSource,
XPathConstants.BOOLEAN);
} catch (XPathExpressionException e) {
throw new BuildException("XPath expression fails", e);
}
return result.booleanValue();
}
示例4: removeWhitespace
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private void removeWhitespace(Document document) throws XPathExpressionException {
document.normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']",
document,
XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
}
}
示例5: doPopulateUnapproved
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private void doPopulateUnapproved(Map<String, ModuleInfo> moduleRATInfo, Element rootElement, XPath path) throws XPathExpressionException {
NodeList evaluate = (NodeList) path.evaluate("descendant::resource[license-approval/@name=\"false\"]", rootElement, XPathConstants.NODESET);
for (int i = 0; i < evaluate.getLength(); i++) {
String resources = relativize(evaluate.item(i).getAttributes().getNamedItem("name").getTextContent());
String moduleName = getModuleName(resources);
if (!moduleRATInfo.containsKey(moduleName)) {
moduleRATInfo.get(NOT_CLUSTER).addUnapproved(resources);
} else {
moduleRATInfo.get(moduleName).addUnapproved(resources);
}
}
}
示例6: readExampleDefine
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public static ExampleDefine readExampleDefine(InputStream in) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbd = dbf.newDocumentBuilder();
Document doc = dbd.parse(in);
XPathFactory f = XPathFactory.newInstance();
XPath path = f.newXPath();
Node scriptNode= (Node)path.evaluate("example/script", doc,XPathConstants.NODE);
String script = scriptNode.getTextContent().trim();
Node contextNode= (Node)path.evaluate("example/context", doc,XPathConstants.NODE);
String context = contextNode.getTextContent().trim();
return new ExampleDefine(script,context);
}
示例7: compare
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* Compares AXI component against corresponding DOM element.
*/
private boolean compare(AXIComponent axiNode) {
FileUtil util = FileUtil.getInstance();
try {
InputSource inputSource = util.openFile(expectedFileURL);
if(inputSource == null) {
success = false;
return false;
}
String expression = getExpression(axiNode);
XPath xpath = XPathFactory.newInstance().newXPath();
Node domNode = (Node) xpath.evaluate(expression, inputSource, XPathConstants.NODE);
if(!axiNode.toString().equals(domNode.getNodeName())) {
success = false;
errorMessage = "Expected AXI node " + axiNode + ", but found DOM node " + domNode.getNodeName();
return false;
}
if(!compareChildren(axiNode, domNode)) {
return false;
}
} catch(Exception ex) {
ex.printStackTrace();
success = false;
errorMessage = "Exception: " + ex.getMessage();
return false;
} finally {
util.closeFile();
}
return true;
}
示例8: findElementValue
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private ElementValue findElementValue(ElementValue pev, String path, String defaultvalue) {
XPath xpath = XPathFactory.newInstance().newXPath();
try {
Element el = (Element) xpath.evaluate(path, pev.getElement(), XPathConstants.NODE);
return el == null
? new ElementValue(defaultvalue)
: new ElementValue(el, pev.isInHerited() ? Type.INHERITED : Type.OK);
} catch (XPathExpressionException ex) {
return new ElementValue(defaultvalue);
}
}
示例9: stringTag
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private String stringTag(
final Element element,
final XPath xpath,
final String tagName
) throws XPathExpressionException {
return (String) xpath.evaluate(tagName + "/text()", element, STRING);
}
示例10: getNodeList
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* Hulpmethode om de nodes uit een node te halen middels xpath.
* @param locatie de locatie van de node als xpath.
* @param xPath een XPath instantie
* @param node de basis node
* @return de text
*/
protected static NodeList getNodeList(final String locatie, final XPath xPath, final Node node) {
try {
return (NodeList) xPath.evaluate(locatie, node, XPathConstants.NODESET);
} catch (final XPathExpressionException e) {
LOGGER.error("XPath voor node list kon niet worden geëvalueerd voor locatie {}.", locatie);
throw new UnsupportedOperationException(e);
}
}
示例11: findElement
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private Element findElement(ElementValue pev, String path) {
XPath xpath = XPathFactory.newInstance().newXPath();
try {
return (Element) xpath.evaluate(path, pev.getElement(), XPathConstants.NODE);
} catch (XPathExpressionException ex) {
return null;
}
}
示例12: getVersionElement
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private Element getVersionElement(Document doc) throws MojoExecutionException {
XPath xPath = XPathFactory.newInstance().newXPath();
try {
NodeList nodes = ((NodeList) xPath.evaluate("/project/version", doc.getDocumentElement(), XPathConstants.NODESET));
if (nodes.getLength() == 0) {
return null;
}
return (Element) nodes.item(0);
} catch (XPathExpressionException e) {
throw new MojoExecutionException("Failed to evaluate xpath expression", e);
}
}
示例13: getAttributeQuery
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private Element getAttributeQuery(Document document) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContextImpl context = new NamespaceContextImpl();
context.startPrefixMapping("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
context.startPrefixMapping("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
xpath.setNamespaceContext(context);
return (Element) xpath.evaluate("//samlp:Response", document, XPathConstants.NODE);
}
示例14: getXPathFromLastRESTResponse
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
@Then("get value from REST-XML response by '$xpath' and save to '$variable'")
public void getXPathFromLastRESTResponse(String xPath, String variable) throws IOException, SOAPException, ParserConfigurationException, SAXException, XPathExpressionException {
Response response = getVariableValue(KEY);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource source = new InputSource();
source.setCharacterStream(new StringReader(response.getBody().asString()));
Document doc = db.parse(source);
XPath xpath = XPathFactory.newInstance().newXPath();
Node node = (Node) xpath.evaluate(xPath, doc.getDocumentElement(), XPathConstants.NODE);
assertNotNull("No element by Xpath: " + xPath + " was found", node);
save(variable, node.getTextContent());
}
示例15: hasVersionCode
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* Returns whether the version Code attribute is set in a given manifest.
* @param manifestFile the manifest to check
* @return true if the versionCode attribute is present and its value is not empty.
* @throws XPathExpressionException
* @throws StreamException If any error happens when reading the manifest.
*/
public static boolean hasVersionCode(IAbstractFile manifestFile)
throws XPathExpressionException, StreamException {
XPath xPath = AndroidXPathFactory.newXPath();
InputStream is = null;
try {
is = manifestFile.getContents();
Object result = xPath.evaluate(
"/" + NODE_MANIFEST +
"/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
":" + ATTRIBUTE_VERSIONCODE,
new InputSource(is),
XPathConstants.NODE);
if (result != null) {
Node node = (Node)result;
if (!node.getNodeValue().isEmpty()) {
return true;
}
}
} finally {
try {
Closeables.close(is, true /* swallowIOException */);
} catch (IOException e) {
// cannot happen
}
}
return false;
}