Example usage for org.w3c.dom Element getAttributes

List of usage examples for org.w3c.dom Element getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.out.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());//w w w.j  a  v  a  2 s . com
    }
}

From source file:Main.java

License:asdf

public static void dom(Element element) {
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        System.out.println(attrName);
        String attrValue = attr.getNodeValue();
        System.out.println(attrValue);
    }/*  www .j av a 2s . c o  m*/
}

From source file:Main.java

public static Attr[] getAttrs(Element elem) {
    NamedNodeMap attrMap = elem.getAttributes();
    Attr[] attrArray = new Attr[attrMap.getLength()];
    for (int i = 0; i < attrMap.getLength(); i++)
        attrArray[i] = (Attr) attrMap.item(i);
    return attrArray;
}

From source file:Main.java

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());
    }/*from  w w  w  .  j a v a 2s .c om*/
    return attrs;
}

From source file:Main.java

public static void copyAttr(Element from, Element to) {
    NamedNodeMap nnm = from.getAttributes();
    for (int i = 0; i < nnm.getLength(); ++i) {
        String name = nnm.item(i).getNodeName();
        if (!to.hasAttribute(name)) {
            String value = nnm.item(i).getNodeValue();
            to.setAttribute(name, value);
        }//from   ww  w.  java 2  s . c om
    }
}

From source file:Main.java

public static boolean hasAttribute(Element element, String value) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        if (value.equals(node.getNodeValue())) {
            return true;
        }/*from   ww  w . j  av a 2 s. com*/
    }
    return false;
}

From source file:Main.java

/**
 * Gets the names of all the attributes of the given {@link Element}.
 *
 * @param element The {@link Element} to get the attribute names of.
 * @return Returns an array, possibly empty, of the attribute names.
 *//*w w w  .  j  a  va 2s.  c  o m*/
public static String[] getAttributesNamesOf(Element element) {
    final NamedNodeMap map = element.getAttributes();
    final int numAttributes = map.getLength();
    final String[] result = new String[numAttributes];
    for (int i = 0; i < numAttributes; ++i)
        result[i] = map.item(i).getNodeName();
    return result;
}

From source file:Utils.java

public static void copyAttributes(Element from, Element to) {
    NamedNodeMap attributes = from.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr node = (Attr) attributes.item(i);
        to.setAttributeNS(node.getNamespaceURI(), node.getName(), node.getValue());
    }/*  ww  w  . j  ava2 s.  c  o  m*/
}

From source file:Main.java

private static void addAttrDirect(Map<String, Object> values, Element el) {
    NamedNodeMap attrs = el.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        values.put(attr.getNodeName(), attr.getNodeValue());
    }/*from  w  ww.  j  a v  a  2  s.  c  om*/
}

From source file:Main.java

/**
 * copy all attributes form one element to another
 * @param fromEl/*from  ww w . ja  v a 2 s . c o m*/
 * @param toEl
 */
public static void copyAttributes(Element fromEl, Element toEl) {
    for (int a = 0; a < fromEl.getAttributes().getLength(); a++) {
        Attr at = (Attr) fromEl.getAttributes().item(a);
        toEl.setAttribute(at.getName(), at.getValue());
    }
}