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

/**
 * @param sampleNode//from w  w w.  j  av  a 2 s.com
 * @return The time and time unit from a sample. e.g. "day 1" or "week 2".
 *         Returns "" if no time information is avalilable.
 */
public static String getSampleTime(Node sampleNode) {
    String time;
    if (sampleNode.getAttributes() == null || sampleNode.getAttributes().getNamedItem("time") == null)
        time = "-1";
    else
        time = sampleNode.getAttributes().getNamedItem("time").getFirstChild().getNodeValue();
    String timeunit;
    if (sampleNode.getAttributes() == null || sampleNode.getAttributes().getNamedItem("unit") == null)
        timeunit = "-1";
    else
        timeunit = sampleNode.getAttributes().getNamedItem("unit").getFirstChild().getNodeValue();

    String timeUnitAndTime = timeunit + " " + time; // +" ("+unit+")";
    if (time.equals("-1") && timeunit.equals("-1"))
        timeUnitAndTime = ""; // "("+unit+")";

    return timeUnitAndTime;
}

From source file:Main.java

/**
 * @param n Node to examine//ww  w . j a  v  a  2 s.co m
 * @param attr Attribute to look for
 * @param def Default value to return if attribute is not present
 * @return if the Node contains the named Attribute, the value, if not, the def parameter
 */
public static String getAttributeIgnoreCase(Node n, String attr, String def) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null)
        return def;
    for (int i = 0; i < attrs.getLength(); i++) {
        Node ret = attrs.item(i);
        if (ret.getNodeName().equalsIgnoreCase(attr))
            return ret.getNodeValue();
    }
    return def;
}

From source file:Main.java

/** returns the value of the named attribute, or the specified default if the attribute
 *  value is null./*  ww w .j  a va  2  s  . c  om*/
 *  @param Node the node whose attribute should be retrieved.
 *  @param String the name of attribute whose value should be retrieved.
 *  @param String 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 String 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

/**
 * Gets an attribute value or an empty string if not found
 * /* w w  w. j  av  a2 s .  c  o  m*/
 * @param node Node with the attribute
 * @param attrName Name of the attribute
 * @return Value of the attribute or empty string if not found
 */
public static String getAttribute(Node node, String attrName) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null)
        return "";
    Node attr = attributes.getNamedItem(attrName);

    if (attr != null) {
        return attr.getNodeValue();
    } else {
        return "";
    }
}

From source file:Main.java

public static void getNamespaces(Node node, Map<String, String> list) {
    NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node n = atts.item(i);/*from  w  ww  .  j  av  a 2  s. c  om*/
        if ("xmlns".equals(n.getNodeName())) {
            list.put(n.getNodeName(), n.getNodeValue());
        } else {
            if (n.getNodeName().startsWith("xmlns:")) {
                list.put(n.getNodeName().substring(6), n.getNodeValue());
            }
        }
    }
}

From source file:Main.java

public static void setAllIdentityAttribute(Document document, String attributeValue) {
    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");
            }/*from  w  w w.  j a va2 s.  c  om*/
        }
    }
}

From source file:Main.java

private static boolean hasEqualAttributes(Node arg0, Node arg) {

    NamedNodeMap map1 = arg0.getAttributes();
    NamedNodeMap map2 = arg.getAttributes();
    int len = map1.getLength();
    if (len != map2.getLength()) {
        return false;
    }//  ww w  . j  ava 2s .  c  om

    for (int i = 0; i < len; i++) {
        Node n1 = map1.item(i);
        if (n1.getNodeName() != null) {
            Node n2 = map2.getNamedItem(n1.getNodeName());
            if (n2 == null) {
                return false;
            } else if (!n1.getNodeValue().equals(n2.getNodeValue())) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name, String def) {
    return getNamedItemNodeValue(node.getAttributes(), name, def);
}

From source file:Main.java

/**
 * Returns null if the attribute doesn't exist, otherwise returns the attribute.
 * @param node//w w  w  .j ava2  s.co  m
 * @param attribute
 * @return
 */
public static String getAttribute(Node node, String attribute) {
    Node attributeNode = node.getAttributes().getNamedItem(attribute);

    if (attributeNode == null) {
        return null;
    }
    return node.getNodeValue();
}

From source file:Main.java

/**
 * Convenience method to retrieve an attribute of an element.
 * Note that calling element.getAttributes() once may be more efficient when pulling several attributes.
 *
 * @param element - element to retrieve the attribute from.
 * @param attr - attribute to get value.
 * @return attribute value or {@code null}
 *///from   w  w w  . j  a v a2 s .com
public static String determineAttributeValue(Node element, String attr) {
    NamedNodeMap attributes = element.getAttributes();
    return attributes != null ? determineNodeValue(attributes.getNamedItem(attr)) : null;
}