本文整理汇总了Java中javax.xml.xpath.XPathExpression.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Java XPathExpression.evaluate方法的具体用法?Java XPathExpression.evaluate怎么用?Java XPathExpression.evaluate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.xpath.XPathExpression
的用法示例。
在下文中一共展示了XPathExpression.evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isMavenFXProject
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
public static boolean isMavenFXProject(@NonNull final Project prj) {
if (isMavenProject(prj)) {
try {
FileObject pomXml = prj.getProjectDirectory().getFileObject("pom.xml"); //NOI18N
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(FileUtil.toFile(pomXml));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression exprJfxrt = xpath.compile("//bootclasspath[contains(text(),'jfxrt')]"); //NOI18N
XPathExpression exprFxPackager = xpath.compile("//executable[contains(text(),'javafxpackager')]"); //NOI18N
XPathExpression exprPackager = xpath.compile("//executable[contains(text(),'javapackager')]"); //NOI18N
boolean jfxrt = (Boolean) exprJfxrt.evaluate(doc, XPathConstants.BOOLEAN);
boolean packager = (Boolean) exprPackager.evaluate(doc, XPathConstants.BOOLEAN);
boolean fxPackager = (Boolean) exprFxPackager.evaluate(doc, XPathConstants.BOOLEAN);
return jfxrt && (packager || fxPackager);
} catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException ex) {
LOGGER.log(Level.INFO, "Error while parsing pom.xml.", ex); //NOI18N
return false;
}
}
return false;
}
示例2: getNodeList
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
public static Iterator<Node> getNodeList(String exp, Element dom) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expUserTask = xpath.compile(exp);
final NodeList nodeList = (NodeList) expUserTask.evaluate(dom, XPathConstants.NODESET);
return new Iterator<Node>() {
private int index = 0;
@Override
public Node next() {
return nodeList.item(index++);
}
@Override
public boolean hasNext() {
return (nodeList.getLength() - index) > 0;
}
};
}
示例3: countNodes
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* Count nodes which are given via xpath expression
*/
public static Double countNodes(Node node, String nodePath)
throws XPathExpressionException {
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
final XPathExpression expr = xpath.compile("count(" + nodePath + ')');
return (Double) expr.evaluate(node, XPathConstants.NUMBER);
}
示例4: findFunction
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* @param xmlString
* @return
* @throws Exception
*/
public static String findFunction(String xmlString) throws Exception {
DocumentBuilder document = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element node = document.parse(new ByteArrayInputStream(xmlString.getBytes())).getDocumentElement();
//Xpath
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='Envelope']/*[local-name()='Body']/*");
Object result = expr.evaluate(node, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
if (log.isDebugEnabled()) {
log.debug("nodes.item(0).getNodeName():" + nodes.item(0).getNodeName());
}
if (nodes.item(0).getNodeName().contains("query")) {
return "query";
} else if (nodes.item(0).getNodeName().contains("update")) {
return "update";
} else {
return null;
}
}
示例5: parsePackageName
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* parse package name
*
* @param androidManifestFullFilename
* @return String
*/
public static String parsePackageName(String androidManifestFullFilename) {
String packageName = null;
if (FileUtil.isExist(androidManifestFullFilename)) {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
Document document = JavaXmlUtil.parse(androidManifestFullFilename);
try {
XPathExpression expression = xpath.compile("/manifest[@package]");
NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
if (nodeList != null && nodeList.getLength() > 0) {
Node node = nodeList.item(0);
packageName = node.getAttributes().getNamedItem("package").getTextContent();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return packageName;
}
示例6: getModsIdentifiersValidOnly
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
private Map<String, Map<String, Set<String>>> getModsIdentifiersValidOnly(Document doc, ValidationResult validationResult) throws InvalidXPathExpressionException, XPathExpressionException {
Map<String, Map<String, Set<String>>> result = new HashMap<>();
XPathExpression dmdSecXpath = engine.buildXpath("/mets:mets/mets:dmdSec[starts-with(@ID, \"MODSMD\")]");
NodeList dmdSecNodes = (NodeList) dmdSecXpath.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < dmdSecNodes.getLength(); i++) {
Element dmdSecEl = (Element) dmdSecNodes.item(i);
String id = dmdSecEl.getAttribute("ID").substring("MODSMD_".length());
Map<String, Set<String>> ids = new HashMap<>();
XPathExpression idXpath = engine.buildXpath("mets:mdWrap/mets:xmlData/mods:mods/mods:identifier[not(@invalid='yes')]");
NodeList idEls = (NodeList) idXpath.evaluate(dmdSecEl, XPathConstants.NODESET);
for (int j = 0; j < idEls.getLength(); j++) {
Element idEl = (Element) idEls.item(j);
String type = idEl.getAttribute("type");
String value = idEl.getTextContent().trim();
Set<String> values = ids.get(type);
if (values == null) {
values = new HashSet<>();
ids.put(type, values);
}
values.add(value);
}
result.put(id, ids);
}
return result;
}
开发者ID:NLCR,项目名称:komplexni-validator,代码行数:26,代码来源:VfCheckPrimaryMetsDcIdentifiersMatchModsIdentifiers.java
示例7: evaluate
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* @param node
* @param exp
* @param returnType XPathConstants.NODESET
* @return
*/
public Object evaluate(Node node, String exp, QName returnType) {
try {
XPathExpression expr = getExpr(exp);
if (expr != null) {
return expr.evaluate(node, returnType);
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return null;
}
示例8: getCoordinates
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**@param{Object} Address- The physical address of a location
* This method returns Geocode coordinates of the Address object.Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway,
* Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739).Please give all the physical Address values
* for a precise coordinate. setting none of the values will give a null value for the Latitude and Longitude.
*/
public static GeoCode getCoordinates(Address address) throws Exception
{
GeoCode geo= new GeoCode();
String physicalAddress = (address.getAddress1() == null ? "" : address.getAddress1() + ",")
+ (address.getAddress2() == null ? "" : address.getAddress2() + ",")
+ (address.getCityName() == null ? "" : address.getCityName() + ",")
+ (address.getState() == null ? "" : address.getState() + "-")
+ (address.getZip() == null ? "" : address.getZip() + ",")
+ (address.getCountry() == null ? "" : address.getCountry());
String api = GMAPADDRESS + URLEncoder.encode(physicalAddress, "UTF-8") + SENSOR;
URL url = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.connect();
int responseCode = httpConnection.getResponseCode();
if(responseCode == 200)
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(httpConnection.getInputStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(STATUS);
String status = (String)expr.evaluate(document, XPathConstants.STRING);
if(status.equals("OK"))
{
expr = xpath.compile(LATITUDE);
String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile(LONGITUDE);
String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
geo.setLatitude(latitude);
geo.setLongitude(longitude);
}
}
else
{
throw new Exception("Fail to Convert to Geocode");
}
return geo;
}
示例9: testNodeList
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
@Test
public void testNodeList() {
int n = 5;
while (0 != (n--))
;
System.out.println("n=" + n);
try {
String testXML = "<root>" + " <node/>" + " <node/>" + " <node/>" + " <node/>" + "</root>\n";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// dbf.setNamespaceAware(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(testXML.getBytes());
Document testDoc = builder.parse(bis);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expr = xpath.compile("/root/node");
NodeList testNodes = (NodeList) expr.evaluate(testDoc, XPathConstants.NODESET);
// Node list appears to work correctly
System.out.println("testNodes.getLength() = " + testNodes.getLength());
System.out.println("testNodes[0] = " + testNodes.item(0));
System.out.println("testNodes[0] = " + testNodes.item(0));
System.out.println("testNodes.getLength() = " + testNodes.getLength());
// Access past the end of the NodeList correctly returns null
System.out.println("testNodes[testNodes.getLength()] = " + testNodes.item(testNodes.getLength()));
// BUG! First access of valid node after accessing past the end
// incorrectly returns null
if (testNodes.item(0) == null) {
System.out.println("testNodes[0] = null");
Assert.fail("First access of valid node after accessing past the end incorrectly returns null");
}
// Subsequent access of valid node correctly returns the node
System.out.println("testNodes[0] = " + testNodes.item(0));
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例10: pullValueFromStrings
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* Finds key in strings.xml file and returns text value
*
* @param directory Root directory of apk
* @param key String reference (ie @string/foo)
* @return String|null
* @throws AndrolibException
*/
public static String pullValueFromStrings(File directory, String key) throws AndrolibException {
if (! key.contains("@")) {
return null;
}
File file = new File(directory, "/res/values/strings.xml");
key = key.replace("@string/", "");
if (file.exists()) {
try {
Document doc = loadDocument(file);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xPath.compile("/resources/string[@name=" + '"' + key + "\"]/text()");
Object result = expression.evaluate(doc, XPathConstants.STRING);
if (result != null) {
return (String) result;
}
} catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) {
}
}
return null;
}
示例11: getNodeByXPath
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* Returns the node in the given document at the specified XPath.
*
* @param node
* The document to be checked.
* @param xpathString
* The xpath to look at.
* @return The node at the given xpath.
* @throws XPathExpressionException
*/
public static Node getNodeByXPath(Node node, String xpathString)
throws XPathExpressionException {
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new XmlNamespaceResolver(
getOwningDocument(node)));
final XPathExpression expr = xpath.compile(xpathString);
return (Node) expr.evaluate(node, XPathConstants.NODE);
}
示例12: getTextValueByPath
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
public static String getTextValueByPath(Node node, String path) {
XPathExpression expr = compile(path);
try {
return expr.evaluate(node);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: getNumberByXPath
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
/**
* Returns the number value of an XPath evaluation.
*
* @param doc
* The document to be checked.
* @param xpathString
* The XPath to search at.
* @return The number according to the XPath.
* @throws XPathExpressionException
*/
public static Number getNumberByXPath(Document doc, String xpathString)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(xpathString);
Number result = (Number) expr.evaluate(doc, XPathConstants.NUMBER);
return result;
}
示例14: evaluateXPath
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
public static Object evaluateXPath(final XPathExpression expr, final Object rootNode, final QName returnType) {
try {
return expr.evaluate(rootNode, returnType);
} catch (final XPathExpressionException e) {
throw new IllegalStateException("Error while evaluating xpath expression " + expr, e);
}
}
示例15: xPathSearch
import javax.xml.xpath.XPathExpression; //导入方法依赖的package包/类
public static String xPathSearch(String input, String expression) throws Exception {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression = xPath.compile(expression);
Document document = documentBuilder.parse(new ByteArrayInputStream(input.getBytes("UTF-8")));
String output = (String) xPathExpression.evaluate(document, XPathConstants.STRING);
return output == null ? "" : output.trim();
}