org.w3c.dom.Element.getChildNodes()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(282)

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

Element.getChildNodes介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Retrieves all child elements of the given DOM element.
 * @param ele the DOM element to analyze
 * @return a List of child {@code org.w3c.dom.Element} instances
 */
public static List<Element> getChildElements(Element ele) {
  Assert.notNull(ele, "Element must not be null");
  NodeList nl = ele.getChildNodes();
  List<Element> childEles = new ArrayList<>();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element) {
      childEles.add((Element) node);
    }
  }
  return childEles;
}

代码示例来源:origin: spring-projects/spring-framework

public BeanDefinitionHolder decorateBeanDefinitionIfRequired(
    Element ele, BeanDefinitionHolder definitionHolder, @Nullable BeanDefinition containingBd) {
  BeanDefinitionHolder finalDefinition = definitionHolder;
  // Decorate based on custom attributes first.
  NamedNodeMap attributes = ele.getAttributes();
  for (int i = 0; i < attributes.getLength(); i++) {
    Node node = attributes.item(i);
    finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
  }
  // Decorate based on custom nested elements.
  NodeList children = ele.getChildNodes();
  for (int i = 0; i < children.getLength(); i++) {
    Node node = children.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
      finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
    }
  }
  return finalDefinition;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse a list element.
 */
public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) {
  String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = collectionEle.getChildNodes();
  ManagedList<Object> target = new ManagedList<>(nl.getLength());
  target.setSource(extractSource(collectionEle));
  target.setElementTypeName(defaultElementType);
  target.setMergeEnabled(parseMergeAttribute(collectionEle));
  parseCollectionElements(nl, target, bd, defaultElementType);
  return target;
}

代码示例来源:origin: apache/incubator-dubbo

private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
  NodeList nodeList = element.getChildNodes();
  if (nodeList != null && nodeList.getLength() > 0) {
    boolean first = true;
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node node = nodeList.item(i);
      if (node instanceof Element) {
        if (tag.equals(node.getNodeName())
            || tag.equals(node.getLocalName())) {
          if (first) {
            first = false;
            String isDefault = element.getAttribute("default");
            if (StringUtils.isEmpty(isDefault)) {
              beanDefinition.getPropertyValues().addPropertyValue("default", "false");
            }
          }
          BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
          if (subDefinition != null && ref != null && ref.length() > 0) {
            subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
          }
        }
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Extracts the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes into a single
 * String value, excluding Comment nodes. Only exposes actual user-specified
 * text, no default values of any kind.
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
  Assert.notNull(valueEle, "Element must not be null");
  StringBuilder sb = new StringBuilder();
  NodeList nl = valueEle.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node item = nl.item(i);
    if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
      sb.append(item.getNodeValue());
    }
  }
  return sb.toString();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse an array element.
 */
public Object parseArrayElement(Element arrayEle, @Nullable BeanDefinition bd) {
  String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = arrayEle.getChildNodes();
  ManagedArray target = new ManagedArray(elementType, nl.getLength());
  target.setSource(extractSource(arrayEle));
  target.setElementTypeName(elementType);
  target.setMergeEnabled(parseMergeAttribute(arrayEle));
  parseCollectionElements(nl, target, bd, elementType);
  return target;
}

代码示例来源:origin: apache/incubator-dubbo

private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
  NodeList nodeList = element.getChildNodes();
  if (nodeList != null && nodeList.getLength() > 0) {
    boolean first = true;
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node node = nodeList.item(i);
      if (node instanceof Element) {
        if (tag.equals(node.getNodeName())
            || tag.equals(node.getLocalName())) {
          if (first) {
            first = false;
            String isDefault = element.getAttribute("default");
            if (StringUtils.isEmpty(isDefault)) {
              beanDefinition.getPropertyValues().addPropertyValue("default", "false");
            }
          }
          BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
          if (subDefinition != null && ref != null && ref.length() > 0) {
            subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
          }
        }
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Utility method that returns the first child element identified by its name.
 * @param ele the DOM element to analyze
 * @param childEleName the child element name to look for
 * @return the {@code org.w3c.dom.Element} instance, or {@code null} if none found
 */
@Nullable
public static Element getChildElementByTagName(Element ele, String childEleName) {
  Assert.notNull(ele, "Element must not be null");
  Assert.notNull(childEleName, "Element name must not be null");
  NodeList nl = ele.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element && nodeNameMatch(node, childEleName)) {
      return (Element) node;
    }
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse a set element.
 */
public Set<Object> parseSetElement(Element collectionEle, @Nullable BeanDefinition bd) {
  String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = collectionEle.getChildNodes();
  ManagedSet<Object> target = new ManagedSet<>(nl.getLength());
  target.setSource(extractSource(collectionEle));
  target.setElementTypeName(defaultElementType);
  target.setMergeEnabled(parseMergeAttribute(collectionEle));
  parseCollectionElements(nl, target, bd, defaultElementType);
  return target;
}

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

private List<Element> getChildrenByTagName(Element element, String tagName) {
  NodeList children = element.getChildNodes();
  List<Element> elts = new ArrayList<>();
  for (int i = 0; i < children.getLength(); i++) {
    if (children.item(i).getNodeType() == Node.ELEMENT_NODE && tagName.equals(children.item(i).getNodeName())) {
      elts.add((Element) children.item(i));
    }
  }
  return elts;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Retrieves all child elements of the given DOM element that match any of the given element names.
 * Only looks at the direct child level of the given element; do not go into further depth
 * (in contrast to the DOM API's {@code getElementsByTagName} method).
 * @param ele the DOM element to analyze
 * @param childEleNames the child element names to look for
 * @return a List of child {@code org.w3c.dom.Element} instances
 * @see org.w3c.dom.Element
 * @see org.w3c.dom.Element#getElementsByTagName
 */
public static List<Element> getChildElementsByTagName(Element ele, String... childEleNames) {
  Assert.notNull(ele, "Element must not be null");
  Assert.notNull(childEleNames, "Element names collection must not be null");
  List<String> childEleNameList = Arrays.asList(childEleNames);
  NodeList nl = ele.getChildNodes();
  List<Element> childEles = new ArrayList<>();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
      childEles.add((Element) node);
    }
  }
  return childEles;
}

代码示例来源:origin: org.springframework/spring-beans

/**
 * Parse a list element.
 */
public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) {
  String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = collectionEle.getChildNodes();
  ManagedList<Object> target = new ManagedList<>(nl.getLength());
  target.setSource(extractSource(collectionEle));
  target.setElementTypeName(defaultElementType);
  target.setMergeEnabled(parseMergeAttribute(collectionEle));
  parseCollectionElements(nl, target, bd, defaultElementType);
  return target;
}

代码示例来源:origin: hibernate/hibernate-orm

private static String extractContent(Element element, String defaultStr) {
  if ( element == null ) {
    return defaultStr;
  }
  NodeList children = element.getChildNodes();
  StringBuilder result = new StringBuilder("");
  for ( int i = 0; i < children.getLength() ; i++ ) {
    if ( children.item( i ).getNodeType() == Node.TEXT_NODE ||
        children.item( i ).getNodeType() == Node.CDATA_SECTION_NODE ) {
      result.append( children.item( i ).getNodeValue() );
    }
  }
  return result.toString().trim();
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * For a given Element, treats the first child as a text element
 * and returns its value.
 */
public static String getEltText(Element element) {
 try {
  NodeList childNodeList = element.getChildNodes();
  if (childNodeList.getLength() == 0) return "";
  return childNodeList.item(0).getNodeValue();
 } catch (Exception e) {
  log.warning("Exception e=" + e.getMessage() + " thrown calling getEltText on element=" + element);
 }
 return "";
}

代码示例来源:origin: org.springframework/spring-beans

/**
 * Parse a set element.
 */
public Set<Object> parseSetElement(Element collectionEle, @Nullable BeanDefinition bd) {
  String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = collectionEle.getChildNodes();
  ManagedSet<Object> target = new ManagedSet<>(nl.getLength());
  target.setSource(extractSource(collectionEle));
  target.setElementTypeName(defaultElementType);
  target.setMergeEnabled(parseMergeAttribute(collectionEle));
  parseCollectionElements(nl, target, bd, defaultElementType);
  return target;
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Returns the first child whose node type is Element under the given Element.
 */
private static Element getFirstChildElement(Element element) {
 try {
  NodeList nodeList = element.getChildNodes();
  for (int i=0; i<nodeList.getLength(); i++) {
   Node node = nodeList.item(i);
   if (node.getNodeType() == Node.ELEMENT_NODE)
    return (Element) node;
  }
 } catch (Exception e) {
  log.warning("Error getting first child Element for element=" + element+", exception=" + e);
 }
 return null;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse property sub-elements of the given bean element.
 */
public void parsePropertyElements(Element beanEle, BeanDefinition bd) {
  NodeList nl = beanEle.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (isCandidateElement(node) && nodeNameEquals(node, PROPERTY_ELEMENT)) {
      parsePropertyElement((Element) node, bd);
    }
  }
}

代码示例来源:origin: org.springframework/spring-beans

/**
 * Parse an array element.
 */
public Object parseArrayElement(Element arrayEle, @Nullable BeanDefinition bd) {
  String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
  NodeList nl = arrayEle.getChildNodes();
  ManagedArray target = new ManagedArray(elementType, nl.getLength());
  target.setSource(extractSource(arrayEle));
  target.setElementTypeName(elementType);
  target.setMergeEnabled(parseMergeAttribute(arrayEle));
  parseCollectionElements(nl, target, bd, elementType);
  return target;
}

代码示例来源:origin: osmandapp/Osmand

protected static void copyAndReplaceElement(Element oldElement, Element newElement) {
    while(oldElement.getChildNodes().getLength() > 0) {
      newElement.appendChild(oldElement.getChildNodes().item(0));
    }
    NamedNodeMap attrs = oldElement.getAttributes();
    for(int i = 0; i < attrs.getLength(); i++) {
      Node ns = attrs.item(i);
      newElement.setAttribute(ns.getNodeName(), ns.getNodeValue());
    }
    ((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse constructor-arg sub-elements of the given bean element.
 */
public void parseConstructorArgElements(Element beanEle, BeanDefinition bd) {
  NodeList nl = beanEle.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (isCandidateElement(node) && nodeNameEquals(node, CONSTRUCTOR_ARG_ELEMENT)) {
      parseConstructorArgElement((Element) node, bd);
    }
  }
}

相关文章

微信公众号

最新文章

更多