Example usage for org.w3c.dom Attr getName

List of usage examples for org.w3c.dom Attr getName

Introduction

In this page you can find the example usage for org.w3c.dom Attr getName.

Prototype

public String getName();

Source Link

Document

Returns the name of this attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");

    Element purchaseOrder = document.getDocumentElement();

    Attr orderDate = purchaseOrder.getAttributeNode("date");
    System.out.println(orderDate.getValue());

    NamedNodeMap attrs = purchaseOrder.getAttributes();
    int attrsCount = attrs.getLength();

    for (int i = 0; i < attrsCount; i++) {
        Attr item = (Attr) attrs.item(i);
        System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'");
    }//from  www  .j  a v a 2 s  .  c o  m
}

From source file:Main.java

public static Map<String, String> getMapOfAttributes(Node elem) {
    Map<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap m = elem.getAttributes();
    if (m != null) {
        for (int i = 0; i < m.getLength(); ++i) {
            Attr a = (Attr) m.item(i);
            attrs.put(a.getName(), a.getValue());
        }/*from   www .  j a  v a  2 s.  c om*/
    }
    return attrs;
}

From source file:Main.java

private static int countNonNamespaceAttribures(NamedNodeMap attrs) {
    int n = 0;//from  w  w  w . j a  v  a  2 s. c  o  m
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            n++;
        }
    }
    return n;
}

From source file:Main.java

/** Return the attributes from the specified element as a Map */
public static Map<String, String> getAttributesAsMap(Element e) {
    Map<String, String> result = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node n = attrs.item(i);
            if (n instanceof Attr) {
                Attr a = (Attr) n;
                result.put(a.getName(), a.getValue());
            }// ww  w.  j  ava2  s.c  om
        }
    }
    return result;
}

From source file:Main.java

public static ImmutableMap<String, String> attributesOf(Element element) {
    NamedNodeMap map = element.getAttributes();
    ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
    for (int i = 0, size = map.getLength(); i < size; i++) {
        Attr attr = (Attr) map.item(i);
        result.put(attr.getName(), attr.getValue());
    }/*from   w w  w . ja  va 2 s.c o  m*/
    return result.build();
}

From source file:Main.java

/**
 * Indicates if the passed node has the named atribute
 * @param node The node to inspect// w  w  w. java 2s  .  com
 * @param name The name of the attribute to look for
 * @return true if the named attribute exists, false otherwise
 */
public static boolean hasAttribute(final Node node, final String name) {
    if (name == null || node == null)
        return false;
    final NamedNodeMap nnm = node.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * copy all attributes form one element to another
 * @param fromEl//from  w  ww . ja v  a 2s  .  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());
    }
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);

            if (attribute.getName().equals(name)) {
                return attribute.getValue();
            }/*from   w  w  w.  j  a  v a2  s . c  o  m*/

        }
    }

    return null;

}

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

From source file:Main.java

public static HashMap<String, String> convertXmlAttributeToHashMap(NamedNodeMap inputNodeMap) {
    HashMap<String, String> outputHashMap = new HashMap<String, String>();

    for (int i = 0; i < inputNodeMap.getLength(); i++) {
        Attr inputNodeMapAttribute = (Attr) inputNodeMap.item(i);
        outputHashMap.put(inputNodeMapAttribute.getName().trim(), inputNodeMapAttribute.getValue().trim());
    }// w w w .ja va2  s .c o m

    return outputHashMap;
}