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

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

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

XPath.selectNodes介绍

[英]Evaluates an XPath expression and returns the list of selected items.

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 #selectNodes(java.lang.Object) it several times is way more efficient.
[中]计算XPath表达式并返回选定项的列表。
注意:当同一个XPath表达式需要多次应用(在相同或不同的上下文中)时,不应使用此方法,因为它要求在计算之前对表达式进行编译。在这种情况下,#newInstance是一个XPath包装器实例,而#selectNodes(java.lang.Object)是它的几倍,效率要高得多。

代码示例

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

private static boolean setCoverages(Element root, StringBuilder builder) throws JDOMException {
  builder.append("  <coverages>\n");
  List<?> coverages = XPath.selectNodes(root, "coverages/coverage");
  boolean longName = false;
  for (Object cov : coverages) {
    if (cov instanceof Element) {
      if (setCoverage(((Element) cov), builder)) {
        longName = true;
      }
    }
  }
  builder.append("  </coverages>\n");
  return longName;
}

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

private static void getAttributes(
    Set<String> timeAttributes, Set<String> elevationAttributes, Element root)
    throws JDOMException {
  List<?> schemaAttributes = XPath.selectNodes(root, "coverages/coverage/schema/attributes");
  for (Object e : schemaAttributes) {
    if (e instanceof Element) {
      String attributes = ((Element) e).getText();
      String[] attribs = attributes.split(",");
      for (String attrib : attribs) {
        if (attrib.contains(TIME_ATTRIB_TYPE)) {
          String[] nameTypePair = attrib.split(":");
          String name = nameTypePair[0];
          if (!timeAttributes.contains(name)) {
            timeAttributes.add(name);
          }
        } else if (attrib.contains(ELEVATION_ATTRIB_TYPE_FLOAT)
            || attrib.contains(ELEVATION_ATTRIB_TYPE_DOUBLE)) {
          String[] nameTypePair = attrib.split(":");
          String name = nameTypePair[0];
          if (!elevationAttributes.contains(name)) {
            elevationAttributes.add(name);
          }
        }
      }
    }
  }
}

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

/**
 * Evaluates an XPath expression and returns the list of selected
 * items.
 * <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 #selectNodes(java.lang.Object) evaluating} it several
 * times is way more efficient.
 * </p>
 *
 * @param  context   the node to use as context for evaluating
 *                   the XPath expression.
 * @param  path      the XPath expression to evaluate.
 *
 * @return the list of selected items, which may be of types: {@link Element},
 *         {@link Attribute}, {@link Text}, {@link CDATA},
 *         {@link Comment}, {@link ProcessingInstruction}, Boolean,
 *         Double, or String.
 *
 * @throws JDOMException   if the XPath expression is invalid or
 *                         its evaluation on the specified context
 *                         failed.
 */
public static List<?> selectNodes(Object context, String path)
    throws JDOMException {
  return newInstance(path).selectNodes(context);
}

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

private void idCreator() throws JDOMException
{
  List<?> list = xpath.selectNodes(doc.getRootElement());
  logger.debug(list.size()+" sections for idTagging");
  
  for (Iterator<?> iter = list.iterator(); iter.hasNext();)
  {
    Element e = (Element) iter.next();
    if(e.getAttribute("id")==null)
    {
      e.setAttribute("id", "autoId"+autoId);autoId++;
    }
  }
}

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

/**
 * Select nodes through an XPath query and return all hits as a List
 *
 * @param element
 *            root element to start search on
 * @param query
 *            XPath expression
 * @return the list of selected items, which may be of types: {@link Element}, {@link Attribute}, {@link Text},
 *         {@link CDATA}, {@link Comment}, {@link ProcessingInstruction}, Boolean, Double, or String.
 */
public List<?> selectNodes(final Element element, final String query) {
  final XPath xPathExpression = getXPathExpression(query);
  try {
    return xPathExpression.selectNodes(element);
  } catch (final JDOMException e) {
    e.printStackTrace();
  }
  return Collections.emptyList();
}

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

private Element mergeRecursive(Element rootElement, XPath xpath) throws OfxInternalProcessingException
{
  try
  {
    List<?> list = xpath.selectNodes(rootElement);
    logger.debug(list.size()+" sections");
    
    for (Iterator<?> iter = list.iterator(); iter.hasNext();)
    {
      Element e = (Element) iter.next();
      boolean noChilds = (e.getChildren().size()==0);
      boolean noContent = (e.getText().length()==0);
      logger.trace(e.getName()+" "+e.getChildren().size()+" "+e.getText().length());
      if(noChilds && noContent){e.detach();}
      else{e.setText(e.getTextTrim());}
    }
  }
  catch (JDOMException e) {logger.error("",e);}
  return rootElement;
}

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

List< ? > nodes = xp.selectNodes(m_document.getDocument());

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

sbXpDataSeries.append("/ofxchart/ofxchartcontainer[@type='dataseries']");
XPath xPath = XPath.newInstance(sbXpDataSeries.toString());
List<?> lDataSeries = xPath.selectNodes(doc);
logger.debug("xpath.DataSeries = "+lDataSeries.size());
for(int i=1;i<=lDataSeries.size();i++)
  sbXpDataSets.append("/ofxchartcontainer[@type='dataset']");
  xPath = XPath.newInstance(sbXpDataSets.toString());
  List<?> lDataSets = xPath.selectNodes(doc);
  logger.debug("xpath.lDataSets = "+lDataSets.size());
  if(lDataSets!=null && lDataSets.size()>0)

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

XPath xpath = XPath.newInstance( selector );
xpath.addNamespace( "j", J2EE_SCHEMA_25_NAMESPACE );
List<?> nodes = xpath.selectNodes( root );
for( Iterator<?> it = nodes.iterator(); it.hasNext(); )
xpath = XPath.newInstance( selector );
xpath.addNamespace( "j", J2EE_SCHEMA_25_NAMESPACE );
nodes = xpath.selectNodes( root );
for( Iterator<?> it = nodes.iterator(); it.hasNext(); )

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

@Deprecated
  private Element mergeRecursive(Element rootElement) throws OfxInternalProcessingException
  {
    try
    {
      List<?> list = xpath.selectNodes(rootElement);
      logger.debug(list.size()+" external sources in "+rootElement.getName()+" in "+rootFile.getAbsolutePath());
      
      for (Iterator<?> iter = list.iterator(); iter.hasNext();)
      {
        Element childElement = (Element) iter.next();
        String source =childElement.getAttribute("source").getValue();
        File childFile = new File(rootFile.getParentFile(),source);
        logger.trace("Found external in "+rpf.relativate(rootFile.getParentFile(), childFile));
        ExternalContentEagerLoader em = new ExternalContentEagerLoader();
        Element eEx = em.getExternal(childFile);
        eEx.detach();
        int index = childElement.getParentElement().indexOf(childElement);
        childElement.getParentElement().setContent(index, eEx);
        childElement.detach();
      }
    }
    catch (JDOMException e) {logger.error("",e);}
    return rootElement;
  }
}

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

private Element mergeRecursive(Element rootElement, XPath xpath) throws OfxInternalProcessingException
{
  try
  {
    List<?> list = xpath.selectNodes(rootElement);
    logger.debug(list.size()+" sections");
    
    for (Iterator<?> iter = list.iterator(); iter.hasNext();)
    {
      Element e = (Element) iter.next();
      
      int index = e.getParentElement().indexOf(e);
      List<Element> lChilds = new ArrayList<Element>();
      
      if     (e.getName().equalsIgnoreCase(Sections.class.getSimpleName())){lChilds = processSections(e.getChildren());}
      else if(e.getName().equalsIgnoreCase(Section.class.getSimpleName())){lChilds = processSection(e.getChildren());}
      else {throw new OfxInternalProcessingException("Root element <"+e.getName()+"> of Wiki.Processing not expected");}
      
      e.getParentElement().addContent(index, lChilds);
      e.getParentElement().removeContent(e);
    }
  }
  catch (JDOMException e) {logger.error("",e);}
  return rootElement;
}

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

private Element exchangeParagraphByTemplate(Element rootElement, XPath xpath)
{
  try
  {
    List<?> list = xpath.selectNodes(rootElement);
    logger.debug(list.size()+" sections");
    
    for (Iterator<?> iter = list.iterator(); iter.hasNext();)
    {
      Element eTemplate = (Element) iter.next();
      int index = eTemplate.getParentElement().getParentElement().indexOf(eTemplate.getParentElement());
      Element parent = eTemplate.getParentElement().getParentElement();
      eTemplate.detach();
      parent.removeContent(index);
      parent.addContent(index, createExternalTemplate(eTemplate));
    }
  }
  catch (JDOMException e) {logger.error("",e);}
  return rootElement;
}

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

xpath = XPath.newInstance( selector );
xpath.addNamespace( "j", J2EE_SCHEMA_25_NAMESPACE );
List<?> constraints = xpath.selectNodes( root );
xpath = XPath.newInstance( selector );
xpath.addNamespace( "j", J2EE_SCHEMA_25_NAMESPACE );
List<?> roles = xpath.selectNodes( root );

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

public void integrateWikiAsExternal(Document ofxDoc) throws OfxAuthoringException
{
  org.jdom2.Document doc = JaxbUtil.toDocument(ofxDoc);
  
  try
  {
    List<?> list = xpath.selectNodes(doc.getRootElement());
    logger.debug(list.size()+" <wiki:content/> found");
    
    for (Iterator<?> iter = list.iterator(); iter.hasNext();)
    {
      Element eChild = (Element) iter.next();
      
      logger.trace(eChild.getName());
      Content wikiContent = (Content)JDomUtil.toJaxb(eChild, Content.class);
      
      Element eOfx = processWikiContent(wikiContent);
      wikiContent.setSource(eOfx.getAttributeValue("source"));
      wikiQueries.getContent().add(wikiContent);
      
      int index = eChild.getParentElement().indexOf(eChild);
      eChild.getParentElement().addContent(index,eOfx);
      eChild.detach();
    }
  }
  catch (JDOMException e) {logger.error("",e);}
  ofxDocWithWikisAsExternal = (Document)JDomUtil.toJaxb(doc, Document.class);
}

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

private void process() throws JDOMException
  List<?> list = xpathCode.selectNodes(rootElement);
  logger.debug(list.size()+" <code> elements found in "+rootElement.getName());
  for (Iterator<?> iter = list.iterator(); iter.hasNext();)

相关文章

微信公众号

最新文章

更多