Example usage for org.w3c.dom NamedNodeMap getNamedItem

List of usage examples for org.w3c.dom NamedNodeMap getNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getNamedItem.

Prototype

public Node getNamedItem(String name);

Source Link

Document

Retrieves a node specified by name.

Usage

From source file:Main.java

public static String getAttribute(Node node, String name) {
    NamedNodeMap namedNodeMap = node.getAttributes();
    if (namedNodeMap == null) {
        return null;
    }/*from www  .  j a v a 2s. com*/
    Node attrNode = namedNodeMap.getNamedItem(name);
    if (attrNode == null) {
        return null;
    }
    return attrNode.getNodeValue();
}

From source file:Main.java

public static String getNodeAttributeValue(String attrName, Node node) {
    if (node == null || attrName == null) {
        return null;
    }//from   w ww  .  j a  v  a  2 s. com

    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null) {
        return null;
    }

    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode != null) {
        return attrNode.getNodeValue();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get string value of specified attribute. If attribute isn't defined return defValue.
 * @param attribs NamedNodeMap/*from   w  w  w.  j  a v a 2  s .  c  o  m*/
 * @param attributeName String
 * @param defValue String
 * @return String
 * @throws DOMException
 */
public static String getAttributeValue(NamedNodeMap attribs, String attributeName, String defValue)
        throws DOMException {
    if (attribs.getNamedItem(attributeName) != null) {
        return attribs.getNamedItem(attributeName).getNodeValue();
    } else {
        return defValue;
    }
}

From source file:Main.java

@CheckForNull
public static Node nodeAttribute(Node node, String attribute) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null) {
        return null;
    }// ww  w  . jav a 2s.com
    return attributes.getNamedItem(attribute);
}

From source file:Main.java

/**
 * A safe way of getting attribute value of attribute with given name. If attribute with given name
 * doesn't exist, returns null//  www.  j  a v  a2  s.c o  m
 * (instead of NPE).
 * 
 * @param node
 * @param attributeName
 * @return
 */
public static String getAttribute(Node node, String attributeName) {
    if (node == null)
        return null;

    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null)
        return null;

    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute == null)
        return null;

    return attribute.getTextContent();
}

From source file:Main.java

/**
 * Get the value of the attribute with the given name of the first tag found with the given tag name in the given
 * document<br/>.//from w  w  w .j  av  a2 s .  c om
 *
 * @param doc
 * @param tagName
 * @param attributeName
 * @return the value of the attribute with the given name of the node or the empty string, if no node with this name
 * exits in this document or the attribute does not exist
 */
public static String getTagAttributeValue(Document doc, String tagName, String attributeName) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        NamedNodeMap attributes = tagList.item(0).getAttributes();
        if (attributes != null) {
            Node attribute = attributes.getNamedItem(attributeName);
            if (attribute != null) {
                return attribute.getNodeValue().trim();
            }
        }
    }
    return "";
}

From source file:Utils.java

public static String getAttribute(Node element, String attName) {
    NamedNodeMap attrs = element.getAttributes();
    if (attrs == null) {
        return null;
    }//  w  w  w. j av a2s.c  om
    Node attN = attrs.getNamedItem(attName);
    if (attN == null) {
        return null;
    }
    return attN.getNodeValue();
}

From source file:Main.java

public static String getAttributeValue(Node elementNode, String attributeName) {
    if (elementNode == null) {
        return null;
    }/*  www  . jav a 2  s .co  m*/
    NamedNodeMap attributes = elementNode.getAttributes();
    if (attributes == null) {
        return null;
    }
    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute == null) {
        return null;
    }
    return attribute.getNodeValue();
}

From source file:Main.java

/**
 *  Sets a named attribute of a Node//from  w w  w .j a  v a 2 s.  c om
 *  @param node The node
 *  @param attr The name of the attribute to set
 *  @param value The value to assign to the attribute
 */
public synchronized static void setAttribute(Node node, String attr, String value) {
    if (node == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (attr == null)
        throw new IllegalArgumentException("Node attribute argument cannot be null");
    if (value == null)
        throw new IllegalArgumentException("Node attribute value argument cannot be null");

    Node attrN = null;
    NamedNodeMap map = node.getAttributes();
    if (map != null)
        attrN = map.getNamedItem(attr);

    if (attrN == null) {
        attrN = node.getOwnerDocument().createAttribute(attr);
        map.setNamedItem(attrN);
    }

    attrN.setNodeValue(value);
}

From source file:Main.java

/**
 * @return the attribute value or null/*  w ww  . ja v  a2  s. com*/
 */
public static String getAttributeValue(Node node, String attributeName) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null)
        return (null);

    Node tempNode = attributes.getNamedItem(attributeName);
    if (tempNode == null)
        return (null);

    return (tempNode.getNodeValue());
}