org.jdom2.xpath.XPath.selectSingleNode()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中org.jdom2.xpath.XPath.selectSingleNode()方法的一些代码示例,展示了XPath.selectSingleNode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XPath.selectSingleNode()方法的具体详情如下:
包路径:org.jdom2.xpath.XPath
类名称:XPath
方法名:selectSingleNode

XPath.selectSingleNode介绍

[英]Evaluates the wrapped XPath expression and returns the first entry in the list of selected nodes (or atomics).

Note: This method should not be used when the same XPath expression needs to be applied several times (on the same or different contexts) as it requires the expression to be compiled before being evaluated. In such cases, #newInstance an XPath wrapper instance and #selectSingleNode(java.lang.Object) it several times is way more efficient.
[中]计算包装的XPath表达式,并返回所选节点(或原子)列表中的第一个条目。
注意:当同一个XPath表达式需要多次应用(在相同或不同的上下文中)时,不应使用此方法,因为它要求在计算之前对表达式进行编译。在这种情况下,#newInstance是一个XPath包装器实例,而#selectSingleNode(java.lang.Object)是它的几倍,效率要高得多。

代码示例

代码示例来源:origin: simpligility/android-maven-plugin

Object source = path.selectSingleNode( r.getRootElement() );
if ( !( source instanceof Element ) )
      xpath, source ) );
Object target = path.selectSingleNode( doc.getRootElement() );
if ( !( target instanceof Element ) )

代码示例来源:origin: geotools/geotools

private static boolean setCoverage(Element cov, StringBuilder builder) throws JDOMException {
  builder.append("    <coverage>\n");
  Element name = (Element) XPath.selectSingleNode(cov, "name");
  String coverageName = name.getText();
  builder.append("      <name>" + coverageName + "</name>\n");
  Element schema = (Element) XPath.selectSingleNode(cov, "schema");
  String schemaName = schema.getAttributeValue("name");
  builder.append("      <schema name=\"" + schemaName + "\" >\n");
  Element schemaAttributesElement = (Element) XPath.selectSingleNode(schema, "attributes");
  String schemaAttribs = schemaAttributesElement.getText();
  schemaAttribs =
      schemaAttribs.replace("imageindex:Integer", "imageindex:Integer,location:String");
  builder.append("        <attributes>" + schemaAttribs + "</attributes>\n");
  builder.append("      </schema>\n");
  addDomainsToCoverage(schemaAttribs, builder);
  builder.append("    </coverage>\n");
  return coverageName.length() > 62;
}

代码示例来源:origin: org.jdom/jdom

/**
 * Evaluates the wrapped XPath expression and returns the first
 * entry in the list of selected nodes (or atomics).
 * <p>
 * <strong>Note</strong>: This method should not be used when the
 * same XPath expression needs to be applied several times (on the
 * same or different contexts) as it requires the expression to be
 * compiled before being evaluated.  In such cases,
 * {@link #newInstance allocating} an XPath wrapper instance and
 * {@link #selectSingleNode(java.lang.Object) evaluating} it
 * several times is way more efficient.
 * </p>
 *
 * @param  context   the element to use as context for evaluating
 *                   the XPath expression.
 * @param  path      the XPath expression to evaluate.
 *
 * @return the first selected item, which may be of types: {@link Element},
 *         {@link Attribute}, {@link Text}, {@link CDATA},
 *         {@link Comment}, {@link ProcessingInstruction}, Boolean,
 *         Double, String, or <code>null</code> if no item was selected.
 *
 * @throws JDOMException   if the XPath expression is invalid or
 *                         its evaluation on the specified context
 *                         failed.
 */
public static Object selectSingleNode(Object context, String path)
    throws JDOMException {
  return newInstance(path).selectSingleNode(context);
}

代码示例来源:origin: org.openfuxml/ofx-wiki

protected double getChartValue(String xp, String type, Document doc)
  {
    double value=0;
    try
    {
      XPath xPath = XPath.newInstance(xp+"[@type='"+type+"']");
      Element element = (Element)xPath.selectSingleNode(doc);
      value = new Double(element.getTextTrim());
    }
    catch (JDOMException e) {logger.error("",e);}
    return value;
  }
}

代码示例来源:origin: Renanse/Ardor3D

/**
 * Select nodes through an XPath query and returns the first hit
 *
 * @param element
 *            root element to start search on
 * @param query
 *            XPath expression
 * @return the first selected item, which may be of types: {@link Element}, {@link Attribute}, {@link Text},
 *         {@link CDATA}, {@link Comment}, {@link ProcessingInstruction}, Boolean, Double, String, or
 *         <code>null</code> if no item was selected.
 */
public Object selectSingleNode(final Element element, final String query) {
  final XPath xPathExpression = getXPathExpression(query);
  try {
    return xPathExpression.selectSingleNode(element);
  } catch (final JDOMException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: org.apache.jspwiki/jspwiki-main

private void printImage( Element base ) throws JDOMException
  Element child = (Element)XPath.selectSingleNode( base, "TBODY/TR/TD/*" );
  if( child == null )

代码示例来源:origin: org.openfuxml/ofx-wiki

Element eP = (Element)xpParent.selectSingleNode(eCode);
if(eP!=null)
  Element ePre = (Element)xpathPre.selectSingleNode(eP);
  if(ePre!=null)

代码示例来源:origin: org.openfuxml/ofx-wiki

public static synchronized Template getTemplate(Templates templates, String name) throws OfxConfigurationException 
{
  Template result = new Template();
  try
  {
    XPath xpath = XPath.newInstance( "//wiki:template[@name='"+name+"']" );
    xpath.addNamespace(Namespace.getNamespace("ofx", "http://www.openfuxml.org"));
    xpath.addNamespace(Namespace.getNamespace("wiki", "http://www.openfuxml.org/wiki"));
    
    Document doc = JaxbUtil.toDocument(templates);
    Element e = (Element)xpath.selectSingleNode(doc);
    if(e!=null){result = (Template)JDomUtil.toJaxb(e, Template.class);}
    else{throw new OfxConfigurationException("No template definition for templateName="+name);}
  }
  catch (JDOMException e) {logger.error("",e);}
  return result;
}

代码示例来源:origin: org.apache.jspwiki/jspwiki-main

Element formName = (Element)XPath.selectSingleNode( e, "INPUT[@name='formname']" );
if( formName != null )

代码示例来源:origin: Unidata/thredds

@Test
@Ignore("WMS not working")
public void checkWMSDates() throws JDOMException, IOException {
 String endpoint = TestOnLocalServer.withHttpPath("/wms/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?service=WMS&version=1.3.0&request=GetCapabilities");
 byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xml);
 Reader in = new StringReader( new String(result, CDM.utf8Charset));
 SAXBuilder sb = new SAXBuilder();
 Document doc = sb.build(in);
 if (show) {
  XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
  fmt.output(doc, System.out);
 }
 XPath xPath = XPath.newInstance("//wms:Dimension");
 xPath.addNamespace("wms", doc.getRootElement().getNamespaceURI());
 Element dimNode = (Element) xPath.selectSingleNode(doc);
 //List<String> content = Arrays.asList(dimNode.getText().trim().split(","));
 List<String> content = new ArrayList<>();
 for (String d : Arrays.asList(dimNode.getText().trim().split(","))) {
  // System.out.printf("Date= %s%n", d);
  CalendarDate cd = CalendarDate.parseISOformat(null, d);
  content.add(cd.toString());
 }
 assertEquals(expectedDatesAsDateTime, content);
}

相关文章

微信公众号

最新文章

更多