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

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

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

Element.getAttributes介绍

暂无

代码示例

代码示例来源:origin: com.thoughtworks.xstream/xstream

public int getAttributeCount() {
  return currentElement.getAttributes().getLength();
}

代码示例来源:origin: alibaba/cobar

public static Map<String, Object> loadAttributes(Element e) {
  Map<String, Object> map = new HashMap<String, Object>();
  NamedNodeMap nm = e.getAttributes();
  for (int j = 0; j < nm.getLength(); j++) {
    Node n = nm.item(j);
    if (n instanceof Attr) {
      Attr attr = (Attr) n;
      map.put(attr.getName(), attr.getNodeValue());
    }
  }
  return map;
}

代码示例来源: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: nutzam/nutz

/**
 * 获取该 XML 元素内所有的属性的值,按照Map的形式返回
 * 
 * @param ele
 *            XML 元素
 * @return 所有属性的值
 */
public static Map<String, String> getAttrs(Element ele) {
  NamedNodeMap nodeMap = ele.getAttributes();
  Map<String, String> attrs = new HashMap<String, String>(nodeMap.getLength());
  for (int i = 0; i < nodeMap.getLength(); i++) {
    attrs.put(nodeMap.item(i).getNodeName(), nodeMap.item(i).getNodeValue());
  }
  return attrs;
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

public String getAttribute(int index) {
  return ((Attr) currentElement.getAttributes().item(index)).getValue();
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

public String getAttributeName(int index) {
  return decodeAttribute(((Attr) currentElement.getAttributes().item(index)).getName());
}

代码示例来源: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: aragozin/jvm-tools

public static String attr(Element el, String... attr) {
    NamedNodeMap nnm = el.getAttributes();
    for(int i = 0; i != nnm.getLength(); ++i) {
      Node node = nnm.item(i);
      if (node instanceof Attr) {
        Attr a = (Attr) node;
        for(String at: attr) {
          if (a.getName().equalsIgnoreCase(at)) {
            return a.getValue();
          }
        }
      }
     }
    return null;
  }
}

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

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-security

private static boolean isDefaultHttpConfig(Element httpElt) {
  return httpElt.getChildNodes().getLength() == 0
      && httpElt.getAttributes().getLength() == 0;
}

代码示例来源:origin: apache/geode

static String findPrefix(final Element root, final String namespaceUri) {
 // Look for all of the attributes of cache that start with xmlns
 NamedNodeMap attributes = root.getAttributes();
 for (int i = 0; i < attributes.getLength(); i++) {
  Node item = attributes.item(i);
  if (item.getNodeName().startsWith("xmlns")) {
   if (item.getNodeValue().equals(namespaceUri)) {
    String[] splitName = item.getNodeName().split(":");
    if (splitName.length > 1) {
     return splitName[1];
    } else {
     return "";
    }
   }
  }
 }
 return null;
}

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

/**
 * Parse the supplied {@link Element} and populate the supplied
 * {@link BeanDefinitionBuilder} as required.
 * <p>This implementation maps any attributes present on the
 * supplied element to {@link org.springframework.beans.PropertyValue}
 * instances, and
 * {@link BeanDefinitionBuilder#addPropertyValue(String, Object) adds them}
 * to the
 * {@link org.springframework.beans.factory.config.BeanDefinition builder}.
 * <p>The {@link #extractPropertyName(String)} method is used to
 * reconcile the name of an attribute with the name of a JavaBean
 * property.
 * @param element the XML element being parsed
 * @param builder used to define the {@code BeanDefinition}
 * @see #extractPropertyName(String)
 */
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  NamedNodeMap attributes = element.getAttributes();
  for (int x = 0; x < attributes.getLength(); x++) {
    Attr attribute = (Attr) attributes.item(x);
    if (isEligibleAttribute(attribute, parserContext)) {
      String propertyName = extractPropertyName(attribute.getLocalName());
      Assert.state(StringUtils.hasText(propertyName),
          "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
      builder.addPropertyValue(propertyName, attribute.getValue());
    }
  }
  postProcess(builder, element);
}

代码示例来源:origin: plutext/docx4j

/**
   * This method throws a CanonicalizationException if the supplied Element
   * contains any relative namespaces.
   *
   * @param ctxNode
   * @throws CanonicalizationException
   * @see C14nHelper#assertNotRelativeNS(Attr)
   */
  public static void checkForRelativeNamespace(Element ctxNode)
    throws CanonicalizationException {
    if (ctxNode != null) {
      NamedNodeMap attributes = ctxNode.getAttributes();

      for (int i = 0; i < attributes.getLength(); i++) {
        C14nHelper.assertNotRelativeNS((Attr) attributes.item(i));
      }
    } else {
      throw new CanonicalizationException("Called checkForRelativeNamespace() on null");
    }
  }
}

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

public static void combineAllApplyTags(Document document) {
  NodeList nl = document.getElementsByTagName("apply");
  while(nl.getLength() > 0) {
    Element app = (Element) nl.item(0);
    Element parent = (Element) app.getParentNode();
    NamedNodeMap attrs = app.getAttributes();
    for(int i = 0; i < attrs.getLength(); i++) {
      Node ns = attrs.item(i);
      parent.setAttribute(ns.getNodeName(), ns.getNodeValue());
    }
    while(app.getChildNodes().getLength() > 0) {
      Node ni = app.getChildNodes().item(0);
      app.getParentNode().insertBefore(ni, app);
    }
    app.getParentNode().removeChild(app);
  }
}

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

public void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  NamedNodeMap attributes = element.getAttributes();
  for (int x = 0; x < attributes.getLength(); x++) {
    Attr attribute = (Attr) attributes.item(x);
    if (isEligibleAttribute(attribute)) {
      String propertyName
          = attribute.getLocalName().endsWith(REF_SUFFIX)
              ? attribute.getLocalName()
                  .substring(0, attribute.getLocalName()
                      .length() - REF_SUFFIX.length())
              : attribute.getLocalName();
      propertyName = Conventions
          .attributeNameToPropertyName(propertyName);
      Assert.state(StringUtils.hasText(propertyName),
          "Illegal property name returned from"
              + " 'extractPropertyName(String)': cannot be"
              + " null or empty.");
      if (attribute.getLocalName().endsWith(REF_SUFFIX)) {
        builder.addPropertyReference(propertyName,
            attribute.getValue());
      } else {
        builder.addPropertyValue(propertyName, attribute.getValue());
      }
    }
  }
}

代码示例来源:origin: groovy/groovy-core

protected boolean printAttributes(Element element) {
  boolean hasAttribute = false;
  NamedNodeMap attributes = element.getAttributes();
  int length = attributes.getLength();
  if (length > 0) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < length; i++) {
      printAttributeWithPrefix((Attr) attributes.item(i), buffer);
    }
    for (int i = 0; i < length; i++) {
      hasAttribute = printAttributeWithoutPrefix((Attr) attributes.item(i), hasAttribute);
    }
    if (buffer.length() > 0) {
      if (hasAttribute) {
        print(", ");
      }
      print(buffer.toString());
      hasAttribute = true;
    }
  }
  return hasAttribute;
}

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

/**
 * checks rule part with tag "sentence"; there is only the "type" attribute right now: checks if sentence type of a token is
 * the same as the value of the type attribute in the rule
 * 
 * @param currentRulePart
 *            currentRulePart
 * @param sentenceType
 *            sentenceType
 * @return true if everything passes
 */
protected boolean checkSentence(Element currentRulePart, String sentenceType) {
  NamedNodeMap attNodes = currentRulePart.getAttributes();
  for (int z = 0; z < attNodes.getLength(); z++) {
    Node el = attNodes.item(z);
    String currentAtt = el.getNodeName();
    String currentVal = el.getNodeValue();
    if (currentAtt.equals("type")) { // there is only the "type" attribute right now
      if (!currentVal.startsWith("!")) { // no negation
        if (!sentenceType.equals(currentVal))
          return false;
      } else { // negation
        currentVal = currentVal.substring(1, currentVal.length());
        if (sentenceType.equals(currentVal))
          return false;
      }
    }
  }
  return true;
}

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

public void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  NamedNodeMap attributes = element.getAttributes();
  for (int x = 0; x < attributes.getLength(); x++) {
    Attr attribute = (Attr) attributes.item(x);
    if (isEligibleAttribute(attribute)) {
      String propertyName
          = attribute.getLocalName().endsWith(REF_SUFFIX)
              ? attribute.getLocalName()
                  .substring(0, attribute.getLocalName()
                      .length() - REF_SUFFIX.length())
              : attribute.getLocalName();
      propertyName = Conventions
          .attributeNameToPropertyName(propertyName);
      Assert.state(StringUtils.hasText(propertyName),
          "Illegal property name returned from"
              + " 'extractPropertyName(String)': cannot be"
              + " null or empty.");
      if (attribute.getLocalName().endsWith(REF_SUFFIX)) {
        builder.addPropertyReference(propertyName,
            attribute.getValue());
      } else {
        builder.addPropertyValue(propertyName, attribute.getValue());
      }
    }
  }
}

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

/**
 * checks rule part with tag "specialPosition"; there is only the "type" attribute right now: checks if specialPosition value
 * of a token is the same as the value of the type attribute in the rule; values: endofvorfeld, endofpar (end of paragraph)
 * 
 * @param currentRulePart
 *            currentRulePart
 * @param specialPositionType
 *            specialPositionType
 * @return true if everything passes
 */
protected boolean checkSpecialPosition(Element currentRulePart, String specialPositionType) {
  NamedNodeMap attNodes = currentRulePart.getAttributes();
  for (int z = 0; z < attNodes.getLength(); z++) {
    Node el = attNodes.item(z);
    String currentAtt = el.getNodeName();
    String currentVal = el.getNodeValue();
    if (currentAtt.equals("type")) { // there is only the "type" attribute right now
      if (!currentVal.startsWith("!")) { // no negation
        if (!specialPositionType.equals(currentVal))
          return false;
      } else { // negation
        currentVal = currentVal.substring(1, currentVal.length());
        if (specialPositionType.equals(currentVal))
          return false;
      }
    }
  }
  return true;
}

代码示例来源:origin: org.netbeans.api/org-openide-util

private static void fixupAttrsSingle(Element e) throws DOMException {
  removeXmlBase(e);
  Map<String, String> replace = new HashMap<String, String>();
  NamedNodeMap attrs = e.getAttributes();
  for (int j = 0; j < attrs.getLength(); j++) {
    Attr attr = (Attr) attrs.item(j);
    if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N
      replace.put(attr.getName(), attr.getValue());
    }
  }
  for (Map.Entry<String, String> entry : replace.entrySet()) {
    e.removeAttribute(entry.getKey());
    e.setAttributeNS(null, entry.getKey(), entry.getValue());
  }
}
private static void removeXmlBase(Element e) {

相关文章

微信公众号

最新文章

更多