Listing All the Attributes of a DOM Element - Java XML

Java examples for XML:XML Attribute

Description

Listing All the Attributes of a DOM Element

Demo Code

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class Main {
  public static void main(String[] argv) throws Exception {
    // Get all the attributes of an element in a map
    Element element = null;/*  w w  w .  j av a  2s.co m*/
    NamedNodeMap attrs = element.getAttributes();

    // Get number of attributes in the element
    int numAttrs = attrs.getLength();

    // Process each attribute
    for (int i = 0; i < numAttrs; i++) {
      Attr attr = (Attr) attrs.item(i);

      // Get attribute name and value
      String attrName = attr.getNodeName();
      String attrValue = attr.getNodeValue();
    }
  }
}

Related Tutorials