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 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 ww  .j  a  va  2  s .c om
    return result.build();
}

From source file:Main.java

public static void removeAttributes(Element element) {
    NamedNodeMap namedNodeMap = element.getAttributes();
    while (namedNodeMap.getLength() > 0) {
        element.removeAttributeNode((Attr) namedNodeMap.item(0));
    }//from  w ww .  ja va2 s  .com
}

From source file:Main.java

/**
 * @param el/*w  ww.ja  v a2s . c o m*/
 * @return the number of element and attribute nodes in an XML tree
 */
public static int nodeCount(Element el) {
    int count = 1 + el.getAttributes().getLength(); // this node and its attributes

    // contributions from child elements
    Vector<Element> childElements = childElements(el);
    for (int i = 0; i < childElements.size(); i++)
        count = count + nodeCount(childElements.get(i));

    return count;
}

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.err.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());/*from  ww  w.  j  a v  a2 s.c  o m*/
    }
}

From source file:Main.java

public static String getNamespaceUriDeclaration(Element ele) {
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) {
            return attr.getTextContent();
        }/*from w  ww.j  a va  2s.co  m*/
    }

    return "";
}

From source file:Main.java

public static String getPrefix(org.w3c.dom.Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }/*from   ww w  . j av a2  s .  c  om*/
    }
    return null;
}

From source file:Main.java

private static void transferAttributes(Element elm, Element elm2) {
    NamedNodeMap attributes = elm.getAttributes();
    for (int c = 0; c < attributes.getLength(); c++) {
        Attr attr = (Attr) attributes.item(c);
        elm2.setAttributeNodeNS((Attr) elm2.getOwnerDocument().importNode(attr, true));
    }//from   w  w  w  . j a  v a2 s.  co m
}

From source file:Main.java

public static Map<String, String> getAttributes(Element e) {
    Map results = new HashMap();
    for (int i = 0; i < e.getAttributes().getLength(); i++) {
        results.put(e.getAttributes().item(i).getNodeName(), e.getAttributes().item(i).getNodeValue());
    }//w w w .  j  a v  a  2s . c  o  m
    return results;
}

From source file:Main.java

public static boolean elementExists(Element properties, String name) {
    NodeList nodeList = properties.getElementsByTagName(PROPERTY_NODE);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (name.equals(element.getAttributes().getNamedItem(NAME_ATTR).getNodeValue()))
            return true;
    }//from ww w  . j a va  2 s  . c o m
    return false;
}

From source file:Main.java

/**
 * Remove all attribute from the specified element
 *///  w w  w .j av a  2 s  . c  o m
public static void removeAllAttributes(Element element) {
    final NamedNodeMap nodeMap = element.getAttributes();

    for (int i = 0; i < nodeMap.getLength(); i++)
        element.removeAttribute(nodeMap.item(i).getNodeName());
}