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:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns an attribute of a XML node. Leading and trailing whitespaces are
 * removed in the attribute./* www .  j  a v  a2  s . c om*/
 * 
 * @param node the XML node.
 * @param name the name of the attribute.
 * @return the attribute or <code>null</code> if the attribute is not
 *         present.
 */
public static String attribute(final Node node, final String name) {
    String value;
    if (node.getAttributes() == null) {
        value = null;
    } else {
        final Node attribute = node.getAttributes().getNamedItem(name);
        if (attribute == null) {
            value = null;
        } else {
            value = trimToNull(attribute.getNodeValue());
        }
    }
    return value;
}

From source file:Main.java

/**
 * Gets a string value of the given attribute
 * @param node//from  w  w w.  j a va2  s .c  o  m
 * @param attrName
 * @param defaultValue
 * @return
 */
public static String getValue(Node node, String attrName, String defaultValue) {
    Node tmp;
    if (node == null)
        return defaultValue;

    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null)
        return defaultValue;

    String value = (tmp = attrs.getNamedItem(attrName)) != null ? tmp.getNodeValue() : null;

    if (value == null)
        return defaultValue;

    return value;
}

From source file:Main.java

public static String attributesStr(Node node) {
    if (node == null) {
        return null;
    }/*from   w  w  w  .  ja v a  2 s .  c om*/
    StringBuffer attributes = new StringBuffer();
    for (int i = 0; i < node.getAttributes().getLength(); i++) {
        attributes.append(node.getAttributes().item(i).getNodeName() + "="
                + node.getAttributes().item(i).getNodeValue() + ", ");
    }
    if (attributes.length() > 1) {
        attributes.delete(attributes.length() - 2, attributes.length());
    } else {
        attributes.append(node.getNodeName() + " has NO attributes.");
    }
    return attributes.toString();
}

From source file:com.autentia.tnt.xml.UtilitiesXML.java

/**
 * Devuelve el valor del atributo "nombre" de un nodo
 * @param nombre/*from  w ww .ja v a 2  s.c om*/
 * @param nodo
 * @return
 */
public static String giveAttributeNode(String name, Node node) {
    NamedNodeMap map = node.getAttributes();
    String value = null;
    if (map != null) {
        Node nodoAt = map.getNamedItem(name);
        if (nodoAt != null)
            value = nodoAt.getNodeValue();
    }
    return value;
}

From source file:Main.java

static String getAttributeValue(final Node node, final String attributeName) {
    if (node == null || attributeName == null) {
        return null;
    }//  ww  w .  ja v a 2  s.  c om

    final NamedNodeMap attrMap = node.getAttributes();
    final Node attrNode = attrMap.getNamedItem(attributeName);
    if (attrNode != null) {
        return attrNode.getNodeValue();
    }
    return null;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attributeName) {
    if (node == null || attributeName == null) {
        return null;
    }//from ww w . j a va2  s .c  o  m

    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null) {
        return null;
    }

    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute != null) {
        return attribute.getNodeValue();
    }
    return null;
}

From source file:Main.java

public static ArrayList<Node> getNodesWithKey(Node parent, String key, Set<String> values,
        boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    Node current;
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);//www  .j  ava2 s .c  o  m
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

public static Vector getElementsByAttribValue(org.w3c.dom.Element element, String attrib, String val) {
    NodeList desElements = element.getElementsByTagName("*");
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }/*from   ww w .j  av  a 2  s  . com*/
        }
    }

    return selElements;
}

From source file:Main.java

public static Vector getElementsByTagAndAttribValue(org.w3c.dom.Element element, String tag, String attrib,
        String val) {
    NodeList desElements = element.getElementsByTagName(tag);
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }/*w  w w. ja  v a2s .  co  m*/
        }
    }

    return selElements;
}

From source file:Main.java

public static void stepThrough(Node start) {
    System.out.println(start.getNodeName() + " = " + start.getNodeValue());

    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
        }//from  ww  w.ja v a 2  s.c om
    }

    for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
        stepThrough(child);
    }
}