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

/**
 * Returns an attribute value of a specified node. If this attribute does
 * not exits a specified default value will be returned.
 * /*  www  . j a  v  a 2s  . c  o  m*/
 * @param node The node containing the attribute.
 * @param attribute The name of the attribute.
 * @param defaultValue The value returned if the attribute does not exist.
 * @return The attribute value.
 */
public static String getAttributeValue(Node node, String attribute, String defaultValue) {
    try {
        return node.getAttributes().getNamedItem(attribute).getTextContent();
    } catch (Exception e) {
        return defaultValue;
    }
}

From source file:Main.java

public static String readAttribute(Node element, String attributeName) {
    if (element == null)
        return null;
    NamedNodeMap attributes = element.getAttributes();
    if (attributes == null)
        return null;
    Node value = attributes.getNamedItem(attributeName);
    if (value == null)
        return null;
    return value.getTextContent();
}

From source file:Main.java

/**
 * Search a child node by type//from w w w .j  av a2s .  co  m
 * 
 * @param parent   the parent node
 * @param nodeName   the node name
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByNameAndType(Node parent, String nodeName, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static String getAttribute(final Node node, final String attributeName) {
    final NamedNodeMap tmpMap = node.getAttributes();
    if (tmpMap == null) {
        return null;
    }/*  ww  w  . j a v a2s .  c  om*/
    final Node tmpNode = tmpMap.getNamedItem(attributeName);
    if (tmpNode == null) {
        return null;
    }
    return tmpNode.getNodeValue();
}

From source file:Main.java

public static long getStartTime(Node p) {
    long time = 0;
    Node current = p;
    while ((current = current.getParentNode()) != null) {
        if (current.getAttributes() != null && current.getAttributes().getNamedItem("begin") != null) {
            time += toTime(current.getAttributes().getNamedItem("begin").getNodeValue());
        }/*w  w w  . j a va 2s .c  o  m*/
    }

    if (p.getAttributes() != null && p.getAttributes().getNamedItem("begin") != null) {
        return time + toTime(p.getAttributes().getNamedItem("begin").getNodeValue());
    }
    return time;
}

From source file:Main.java

public static long getEndTime(Node p) {
    long time = 0;
    Node current = p;
    while ((current = current.getParentNode()) != null) {
        if (current.getAttributes() != null && current.getAttributes().getNamedItem("begin") != null) {
            time += toTime(current.getAttributes().getNamedItem("begin").getNodeValue());
        }//from   w w w .j a  va  2 s  .  c  o m
    }

    if (p.getAttributes() != null && p.getAttributes().getNamedItem("end") != null) {
        return time + toTime(p.getAttributes().getNamedItem("end").getNodeValue());
    }
    return time;
}

From source file:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeValue./* w  ww.ja  v  a  2 s .  com*/
 * @param n Node
 * @param item String
 * @return String
 */
public static String getAttributeValue(Node n, String item) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return StringUtils.EMPTY;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return StringUtils.EMPTY;
    }
    return val;
}

From source file:Main.java

public static String getNodeAttribute(Node n, String name) {
    if (n == null)
        return null;

    NamedNodeMap attr = n.getAttributes();
    Node nameNode = attr.getNamedItem(name);
    if (nameNode != null) {
        String name_value = getNodeValue(nameNode);
        return name_value;
    } else {//from   ww w  .ja  v a  2s. com
        return null;
    }
}

From source file:Main.java

public static Map<String, String> findAttributesValues(Node node) {

    Map<String, String> retval = new HashMap<String, String>();
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            String name = attr.getNodeName();
            String value = attr.getNodeValue();

            retval.put(name, value);/*www.  j  a  v  a2s . c o m*/
        }
    }

    return retval;
}

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//from  w ww  . jav  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();
}