Example usage for org.w3c.dom Node getAttributes

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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 removeNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();
        if (attrs.getNamedItem(name) != null) {
            attrs.removeNamedItem(name);
        }/*from  ww w  .j  a  va  2 s .c  o m*/
    }

}

From source file:Main.java

/**
 * Returns all attributes of the given XML node as a list of name=value strings.
 * Mostly just for debugging./*  w w w. j av a 2  s  . c o  m*/
 * @param node
 * @return
 */
public static List<String> getAttributes(Node node) {
    List<String> result = new ArrayList<String>();
    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attrNode = attrs.item(i);
        result.add(attrNode.getNodeName() + "=" + attrNode.getNodeValue());
    }
    return result;
}

From source file:Main.java

public static void setAttributeValue(Node node, String attribute, String value) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        Node nameAttr = attributes.getNamedItem(attribute);
        if (nameAttr != null) {
            nameAttr.setNodeValue(value);
        }// ww w  .j a v a2s.c o m
    }
}

From source file:Main.java

public static String getAttribute(Node node, String localName, String namespaceURI) {
    Node attributeNode = node.getAttributes().getNamedItemNS(namespaceURI, localName);
    if (attributeNode == null) {
        return null;
    }//w w w.ja  v a2  s  .  c o m
    return attributeNode.getTextContent();
}

From source file:Main.java

public static void dump(Node node) {
    System.out.println("Node: " + node.getNodeName());
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            System.out.println("   " + n.getNodeName() + ":" + n.getNodeValue());
        }//from  w w  w .ja v a2 s.  c  om
    }
}

From source file:org.springsource.ide.eclipse.commons.content.core.util.ContentUtil.java

public static String getAttributeValue(Node node, String attribute) {
    Node item = node.getAttributes().getNamedItem(attribute);
    return (item != null) ? item.getNodeValue() : null;
}

From source file:Main.java

public static Document removeFromRootElement(Document document, String nodeName, String attributeName,
        String attributeValue) {/*from  ww w  .j av a 2s  .  com*/
    try {
        Element rootElement = document.getDocumentElement();

        NodeList nl = document.getElementsByTagName(nodeName);
        Vector<Node> deletedNodes = new Vector<Node>();

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            String noteAttributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();

            if (noteAttributeValue.equals(attributeValue)) {
                deletedNodes.add(node);
            }

        }

        for (Node deletedNode : deletedNodes) {
            rootElement.removeChild(deletedNode);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static ArrayList<String> getAlternativeIDs(Node xmlSubstanceNode) {
    ArrayList<String> result = new ArrayList<String>();
    int maxID = xmlSubstanceNode.getAttributes().getLength();
    for (int i = 0; i < maxID; i++) {
        Attr oAlternative = (Attr) xmlSubstanceNode.getAttributes().getNamedItem("name" + i);
        if (oAlternative != null) {
            result.add(oAlternative.getNodeValue());
        }//from   ww  w.  j  a v  a  2 s.  c om
    }
    return result;
}

From source file:Main.java

public static List<String> getAllAttrValueByName(Node item, String name) {
    List<String> lst = null;
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        if (name.equals(attrName)) {
            if (lst == null) {
                lst = new ArrayList();
            }//from w w w.j  av a  2  s  . com
            lst.add(attr.getNodeValue());
        }
    }
    return lst;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attrName) {
    if (node == null)
        return null;
    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null)
        return null;
    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode == null)
        return null;
    return attrNode.getNodeValue();
}