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 getAttributeFromNode(Node node, String attributeName) {
    if (node == null || attributeName == null)
        return null;
    NamedNodeMap attributes = node.getAttributes();
    Node attribute = attributes.getNamedItem(attributeName);
    String value = null;/*from ww w .  j  a  v  a2 s.  co m*/
    if (attribute != null)
        value = attribute.getNodeValue();
    return value;
}

From source file:Main.java

public static String getOptionalAttributeValue(NamedNodeMap attrs, String name) {

    Node namedItem = attrs.getNamedItem(name);
    if (namedItem == null) {
        return "";
    } else {// w  w w .j a  v a2 s  .c  o  m
        return namedItem.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 av  a  2 s  .co m

    return value;
}

From source file:Main.java

public static String getStringValue(NamedNodeMap values, String name, String defaultValue) {
    Node node = values.getNamedItem(name);
    return node == null ? defaultValue : node.getNodeValue();
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {
    NamedNodeMap map = node.getAttributes();

    Node n = map.getNamedItem(name);
    if (n != null) {
        return n.getNodeValue();
    }/*from   w  ww .j  a  v a2s .  c o m*/
    return null;
}

From source file:Main.java

/**
 * @param node//  w ww.  jav a  2  s. co  m
 * @param attributeKey
 * @return
 */
public static String getAttributeValue(Node node, String attributeKey) {
    NamedNodeMap attributes = node.getAttributes();
    Node attribute = attributes.getNamedItem(attributeKey);
    if (attribute == null) {
        return null;
    }
    return attribute.getNodeValue();
}

From source file:Main.java

public static boolean getBooleanValue(NamedNodeMap values, String name, boolean defaultValue) {
    Node node = values.getNamedItem(name);
    return node == null ? defaultValue : Boolean.valueOf(node.getNodeValue()).booleanValue();
}

From source file:Main.java

public static String getId(Node node) {
    try {//w w w .java  2s  .  c o m
        NamedNodeMap nnm = node.getAttributes();
        Node attrib = nnm.getNamedItem("Id");
        Object ID;
        if (attrib.hasChildNodes()) {
            ID = attrib.getChildNodes().item(0).getNodeValue();
        } else {
            ID = attrib.getNodeValue();
        }
        return ID.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

/**
 * Retrieves the value for a xml Node attribute or a default if not found.
 * /*from w  w w  .j  a va  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

/**
 * Retrieves the value for a xml Node attribute or fails if not found.
 * /*from w  ww . j  a  va  2  s.c  o m*/
 * @param <T> the exception type to throw
 * @param node The Node to fetch the value from.
 * @param name The attribute name.
 * @param e an Exception instance
 * @return the attribute value
 * @throws T
 */
public static <T extends Exception> String getNodeAttributeOrFail(Node node, String name, T e) throws T {
    NamedNodeMap attributes = node.getAttributes();
    Node valueNode = attributes.getNamedItem(name);
    if (valueNode == null)
        throw e;
    return valueNode.getNodeValue();
}