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

public static String getId(Node node) {
    try {/*from  ww w. j a v a  2  s .  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

public static String getAttribute(Node node, String attributeName) {
    Node attribute = node.getAttributes().getNamedItem(attributeName);
    if (attribute == null) {
        return null;
    }/*from  ww  w  . ja  va 2 s  . c  om*/

    return attribute.getTextContent();
}

From source file:Main.java

/**
 * @param node//from w  ww .  j a  va 2 s .c  o  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 getBoolean(Node aNode, String attr, boolean defaultValue) {
    if (aNode.getAttributes().getNamedItem(attr) != null) {
        String docText = aNode.getAttributes().getNamedItem(attr).getNodeValue();
        if (docText.toLowerCase().equals("true")) {
            return true;
        }/*from w ww.ja v  a2 s.  com*/
        if (docText.toLowerCase().equals("false")) {
            return false;
        }
        System.out.println("strange boolean value '" + docText + "'");
        return defaultValue;
    } else {
        return defaultValue;
    }
}

From source file:Main.java

/**
 * @param n Node to examine/*www . j ava 2s  .com*/
 * @param attr Attribute to look for
 * @return true if the Node contains the named Attribute
 */
public static boolean hasAttribute(Node n, String attr) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null) {
        return false;
    }
    Node ret = attrs.getNamedItem(attr);
    return ret != null;
}

From source file:Main.java

private static Map<String, Object> getAttributes(Node node) {
    NamedNodeMap attribs = node.getAttributes();
    int attribCount = attribs.getLength();
    Map<String, Object> map = new LinkedHashMap<>(attribCount);
    for (int j = 0; j < attribCount; j++) {
        Node attrib = attribs.item(j);
        map.put(attrib.getNodeName(), attrib.getNodeValue());
    }/*w w  w . j  a  v  a2 s  . c o  m*/
    return map;
}

From source file:Main.java

public static String getAttribute(final Node n, final String attrName, final String defaultValue) {
    if (n.getAttributes().getNamedItem(attrName) != null) {
        return ((Attr) n.getAttributes().getNamedItem(attrName)).getValue();
    }/*from   w  ww  .ja v a2s.c  o  m*/
    return defaultValue;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attributeName) {
    NamedNodeMap attributes = node.getAttributes();
    if (null != attributes) {
        Node namedItem = attributes.getNamedItem(attributeName);
        if (null != namedItem) {
            return namedItem.getNodeValue();
        }/*  w ww  .  ja v  a 2 s  .  c  o  m*/
    }

    return "";
}

From source file:Main.java

/** Remove stray "xmlns" default namespace element that seems to get left over even after removing namespacing from nodes.
 * @param node node to process//ww w. jav  a  2 s.  c om
 */
public static void removeXmlNsAttribute(final Node node) {
    final NamedNodeMap attr = node.getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        final Node item = attr.item(i);
        if (ATTR_XMLNS.equals(item.getNodeName())) {
            attr.removeNamedItem(ATTR_XMLNS);
            return;
        }
    }
}

From source file:Main.java

/**
 * Gets the value from a node's attribute.
 * //from  w w  w . jav  a2s. c  o m
 * @param node
 *            the node.
 * @param attributeName
 *            the name of the attribute.
 * @return the value of the attribute with the specified name or
 *         <code>null</code> if there is no attribute with that name.
 */
public static Node getAttributeValue(Node node, String attributeName) {
    return (node.hasAttributes()) ? node.getAttributes().getNamedItem(attributeName) : null;

}