本文整理汇总了Java中javax.xml.xpath.XPathExpression类的典型用法代码示例。如果您正苦于以下问题:Java XPathExpression类的具体用法?Java XPathExpression怎么用?Java XPathExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XPathExpression类属于javax.xml.xpath包,在下文中一共展示了XPathExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compile
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
XPathExpression compile(String xPathQuery, NamespaceContext namespaceContext)
{
try
{
XPath xPath = this.xPathConfigurer.getXPath(namespaceContext);
return xPath.compile(xPathQuery);
}
catch (XPathExpressionException ex)
{
throw new FluentXmlProcessingException(ex);
}
}
示例2: 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;
}
示例3: findNodes
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
public static List<Node> findNodes ( final Node node, final String expression ) throws XPathExpressionException
{
final XPath path = xpf.newXPath ();
final XPathExpression expr = path.compile ( expression );
final NodeList nodeList = (NodeList)expr.evaluate ( node, XPathConstants.NODESET );
if ( nodeList == null )
{
return Collections.emptyList ();
}
final List<Node> result = new LinkedList<Node> ();
for ( int i = 0; i < nodeList.getLength (); i++ )
{
result.add ( nodeList.item ( i ) );
}
return result;
}
示例4: getByXpath
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
/**
* Get values matching passed XPath expression
* @param node
* @param expression XPath expression
* @return matching values
* @throws Exception
*/
private static String[] getByXpath( Node node, String expression ) throws Exception {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = xPath.compile(expression);
NodeList nlist = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
int nodeListSize = nlist.getLength();
List<String> values = new ArrayList<String>(nodeListSize);
for (int index = 0; index < nlist.getLength(); index++) {
Node aNode = nlist.item(index);
values.add(aNode.getTextContent());
}
return values.toArray(new String[values.size()]);
}
示例5: 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);
}
示例6: getExpr
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
public XPathExpression getExpr(String exp) {
XPathExpression expr = expCache.get(exp);
if (expr == null) {
try {
expr = xpath.compile(exp);
if (expr != null) {
expCache.put(exp, expr);
}
return expr;
} catch (XPathExpressionException e) {
LOG.error(e.getMessage(), e);
return null;
}
}
return expr;
}
示例7: 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;
}
};
}
示例8: buildXpath
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
private XPathExpression buildXpath(String... path) {
StringBuilder builder = new StringBuilder();
for (String pathSegment : path) {
if (builder.length() > 0) {
builder.append("/");
}
builder.append(pathSegment);
}
try {
return XPATH.newXPath().compile(builder.toString());
} catch (XPathExpressionException e) {
LOGGER.error("Unable to compile xpath '{}'", builder);
throw new RuntimeException("Unable to compile xpath");
}
}
示例9: getAttrNames
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
private static Iterable<String> getAttrNames(InputStream is, XPathExpression expr) {
Set<String> names = Sets.newHashSet();
if (expr != null) {
try {
Document doc = parseStream(is);
if (doc != null) {
NodeList nodelist = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodelist.getLength(); i++) {
String name = nodelist.item(i).getNodeValue();
names.add(name);
}
}
} catch (XPathExpressionException ex) {
LOG.log(Level.WARNING, null, ex);
}
}
return names;
}
示例10: compile
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
/**
* <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 {
com.sun.org.apache.xpath.internal.XPath xpath = new XPath (expression, null,
prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT );
// Can have errorListener
XPathExpressionImpl ximpl = new XPathExpressionImpl (xpath,
prefixResolver, functionResolver, variableResolver,
featureSecureProcessing, useServiceMechanism, featureManager );
return ximpl;
} catch ( javax.xml.transform.TransformerException te ) {
throw new XPathExpressionException ( te ) ;
}
}
示例11: 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;
}
}
示例12: getValueFromStatus
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
/**
* Get value from status XML doc.
*
* @param doc Doc to get value from
* @param elementName Element name to search
* @param attribute Attribute to search
* @return The value found
* @throws XPathExpressionException Error in XPath expression
*/
public static String getValueFromStatus(final Document doc,
final String elementName,
final String attribute)
throws XPathExpressionException {
// Create XPath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
StringBuilder expression = new StringBuilder();
// Build XPath from element name and attribute value (if exists)
expression.append("//").append(elementName);
if (attribute != null) {
expression.append("[@name=\'").append(attribute).append("\']");
}
expression.append("/text()");
XPathExpression xPathExpression = xPath.compile(expression.toString());
// Return result from XPath expression
return xPathExpression.evaluate(doc);
}
示例13: getMaxRId
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
private int getMaxRId(ByteArrayOutputStream xmlStream) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(xmlStream.toByteArray()));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("Relationships/*");
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
String id = nodeList.item(i).getAttributes().getNamedItem("Id").getTextContent();
int idNum = Integer.parseInt(id.substring("rId".length()));
this.maxRId = idNum > this.maxRId ? idNum : this.maxRId;
}
return this.maxRId;
}
示例14: parse
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
@Override
public void parse(Consumer<MnoInfo> consumer) throws Exception {
File fXmlFile = config.toFile();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
XPathExpression xpathMno = XPathFactory.newInstance().newXPath().compile("//*[local-name()='mno']");
XPathExpression xpathMasksMno = XPathFactory.newInstance().newXPath().compile("//*[local-name()='mask']");
NodeList mnoNodes = (NodeList) xpathMno.evaluate(doc, XPathConstants.NODESET);
for (int i=0; i<mnoNodes.getLength(); i++) {
Node node = mnoNodes.item(i);
NamedNodeMap attributes = node.getAttributes();
String country = getValue(attributes.getNamedItem("country"));
String title = getValue(attributes.getNamedItem("title"));
String area = getValue(attributes.getNamedItem("area"));
Set<Mask> masks = new HashSet<>();
NodeList maskNodes = (NodeList) xpathMasksMno.evaluate(doc, XPathConstants.NODESET);
for (int j=0; j<maskNodes.getLength(); j++) {
masks.add(Mask.parse(getValue(maskNodes.item(j))));
}
consumer.accept(new MnoInfo(title, area, country, masks));
}
}
示例15: detectEntityIds
import javax.xml.xpath.XPathExpression; //导入依赖的package包/类
private List<String> detectEntityIds(Document metsDoc, EntityType entityType) throws InvalidXPathExpressionException, XPathExpressionException {
XPathExpression xPathExpression = engine.buildXpath("/mets:mets/mets:dmdSec[contains(@ID, \"" + entityType.getDmdSecCode() + "\")]/@ID");
NodeList idAttrs = (NodeList) xPathExpression.evaluate(metsDoc, XPathConstants.NODESET);
Set<String> set = new HashSet<>(idAttrs.getLength());
for (int i = 0; i < idAttrs.getLength(); i++) {
Attr attr = (Attr) idAttrs.item(i);
String id = attr.getValue();
if (id.startsWith("MODSMD_")) {
id = id.substring("MODSMD_".length());
} else if (id.startsWith("DCMD_")) {
id = id.substring("DCMD_".length());
}
String typePrefix = entityType.getDmdSecCode() + '_';
id = id.substring(typePrefix.length());
set.add(id);
}
return new ArrayList<>(set);
}