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 getNamespaceURI(final org.w3c.dom.Node n, final String prefix) {
    Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
    if (prefixDeclaration != null) {
        // we have found the good NameSpace
        return prefixDeclaration.getNodeValue();
    }/*from  ww w.  j a  va 2s  .  c o  m*/
    // we have found the good NameSpace
    // we look for the NameSpace in the parent Node
    return getNamespaceURI(n.getParentNode(), prefix);
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);

            if (attribute.getName().equals(name)) {
                return attribute.getValue();
            }//from  w  w w .j av a 2 s.com

        }
    }

    return null;

}

From source file:Main.java

/**
 * @param n Node to examine/* ww w .  ja  v a2s  .c  o 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 getAttribute(Node n, String attr, String def) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null)
        return def;
    Node ret = attrs.getNamedItem(attr);
    if (ret == null)
        return def;
    else
        return ret.getNodeValue();
}

From source file:Main.java

/**
 * Extracts an attribute from a node.// w w  w .j a  v a  2 s. com
 *
 * @param node
 * @param attr
 * @param def
 * @return The value of the attribute, or def
 */
public static String getAttribute(Node node, String attr, String def) {
    NamedNodeMap attrs = node.getAttributes();
    Node val = attrs.getNamedItem(attr);
    if (val != null) {
        return val.getNodeValue();
    }
    return def;
}

From source file:Main.java

/**
 * Gets attribute value of a node.//ww  w.ja  v  a  2 s.  c om
 * 
 * @param node
 *            a node
 * @param namespaceURI
 *            attribute namespace URI
 * @param attrName
 *            attribute name
 * @return attribute value
 */
public static String getNodeAttributeValueNS(Node node, String namespaceURI, String attrName) {
    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null) {
        return null;
    }
    Node value = attrs.getNamedItemNS(namespaceURI, attrName);
    if (value == null) {
        return null;
    }
    return value.getNodeValue();
}

From source file:Main.java

private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getAttributes().getNamedItem("id") != null) {
            final String id = node.getAttributes().getNamedItem("id").getNodeValue();
            idMap.put(id, (Element) node);
        }/*from  w  w w  .  ja va  2s .  com*/
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        registerIDs(doc, children.item(i), idMap);
    }

}

From source file:Main.java

public static int readInt(Node node, String attributeName, int def) {
    try {//w  ww  .java  2 s  . c  om
        return Integer.parseInt(node.getAttributes().getNamedItem(attributeName).getNodeValue());
    } catch (Exception ex) {
        return def;
    }
}

From source file:Main.java

public static Map<String, String> getAttributes(Node n) {
    if (n == null)
        return null;
    NamedNodeMap attributes = n.getAttributes();
    if (attributes == null || attributes.getLength() <= 0)
        return null;

    Map<String, String> result = new HashMap<>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        result.put(attr.getNodeName(), attr.getNodeValue());
    }/*from ww  w. j a  v  a2  s.  c  o m*/
    return result;
}

From source file:Main.java

public static String nodeToString(Node node) {
    String s = node.getNodeName() + "( ";
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        for (int i = 0; i < map.getLength(); i++) {
            s += map.item(i).getNodeName() + "=" + map.item(i).getNodeValue() + " ";
        }/*from www . ja  v  a  2 s.  c o m*/
    }
    return s += " )";
}

From source file:Main.java

public static HashMap<String, String> getNodeAttributesMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NamedNodeMap namedNodeMap = node.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node attribute = namedNodeMap.item(i);
        map.put(attribute.getNodeName(), attribute.getNodeValue());
    }//  ww w  . j  a v a  2 s.  co  m

    return map;
}