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:Utils.java

public static String getAttribute(Node element, String attName) {
    NamedNodeMap attrs = element.getAttributes();
    if (attrs == null) {
        return null;
    }//from w  w w.  j ava 2 s  . co m
    Node attN = attrs.getNamedItem(attName);
    if (attN == null) {
        return null;
    }
    return attN.getNodeValue();
}

From source file:Main.java

/**
 * Retrieves the value for a xml Node attribute or a default if not found.
 * /*  w w  w  .j av a 2s .  c  o m*/
 * @param node The Node to fetch the value from.
 * @param name The attribute name.
 * @param defaultValue The default value to return if not found.
 * @return and attribute value or a default if not found.
 */
public static String getNodeAttributeOrDefault(Node node, String name, String defaultValue) {
    NamedNodeMap attributes = node.getAttributes();
    Node valueNode = attributes.getNamedItem(name);
    String value = defaultValue;
    if (valueNode != null)
        value = valueNode.getNodeValue();
    return value;
}

From source file:Main.java

public static void setIdentityAttribute(Document document, String attributeValue) {
    boolean isFound = false;
    NodeList propertyList = document.getElementsByTagName("Property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        Node node = propertyList.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals("name") && attribute.getNodeValue().equals(attributeValue)) {
                Element element = (Element) node;
                element.setAttribute("isIdentity", "true");
                isFound = true;//from w ww  . j  ava  2 s .  c o m
                break;
            }
        }
        if (isFound)
            break;
    }
}

From source file:Main.java

public static String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }//from  w w  w .j  a v a 2s.c  o  m
    }
    return "";
}

From source file:Main.java

public static Boolean getAttributeValueAsBoolean(Node node, String attributeName, Boolean defaultValue) {
    final Node attributeNode = node.getAttributes().getNamedItem(attributeName);

    return attributeNode != null ? Boolean.parseBoolean(attributeNode.getTextContent()) : defaultValue;
}

From source file:Main.java

public static Integer getAttributeValueAsInteger(Node node, String attributeName, Integer defaultValue) {
    final Node attributeNode = node.getAttributes().getNamedItem(attributeName);

    return attributeNode != null ? Integer.parseInt(attributeNode.getTextContent()) : defaultValue;
}

From source file:Main.java

/**
 * returns the value of the named attribute, or the specified default if the attribute
 * value is null./* ww w.  ja  v a  2s. c o  m*/
 *
 * @param pNode    the node whose attribute should be retrieved.
 * @param pName    the name of attribute whose value should be retrieved.
 * @param pDefault the default value to return if a value does not exist for the specified
 *                 attribute, or if the specified attribute is not defined in this Node.
 *
 * @return the value of the attribute.
 */
public static String getAttribute(Node pNode, String pName, String pDefault) {
    if (pNode.getAttributes().getNamedItem(pName) == null) {
        return pDefault;
    }
    return pNode.getAttributes().getNamedItem(pName).getNodeValue();
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null) {
        return null;
    }/*from   w  w w .j a  v  a2 s .  com*/

    Node attributeNode = node.getAttributes().getNamedItem(name);
    if (attributeNode == null) {
        return null;
    }
    return attributeNode.getTextContent();
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    String value = "";

    NamedNodeMap attributes = node.getAttributes();
    Node attribute = attributes.getNamedItem(name);
    if (attribute != null) {
        value = attribute.getNodeValue();
    }//  ww  w . j a  v a 2 s.  c  o  m

    return value;
}

From source file:Main.java

static public String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }/*from w  w w  .  ja  va 2s . c o  m*/
    }
    return "";
}