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 boolean nodeMatchesAttributeFilter(Node node, String str, List<String> list) {
    if (str == null || list == null) {
        return true;
    }//from   w  ww.  j a va  2  s  .  c  o m
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        Node namedItem = attributes.getNamedItem(str);
        if (namedItem != null && list.contains(namedItem.getNodeValue())) {
            return true;
        }
    }
    return false;
}

From source file:de.wikilab.android.friendica01.Notification.java

public static Notification fromXmlNode(Node d) {
    Notification n = new Notification();

    n.href = d.getAttributes().getNamedItem("href").getNodeValue();
    n.name = d.getAttributes().getNamedItem("name").getNodeValue();
    n.url = d.getAttributes().getNamedItem("url").getNodeValue();
    n.photo = d.getAttributes().getNamedItem("photo").getNodeValue();
    n.date = d.getAttributes().getNamedItem("date").getNodeValue();
    n.seen = d.getAttributes().getNamedItem("seen").getNodeValue();

    n.content = d.getTextContent();//  w  w w. j a va  2  s .  com

    if (n.content.startsWith("&rarr;")) {
        n.isUnread = true;
        n.content = n.content.substring(6);
    }

    return n;
}

From source file:Main.java

public static List<Node> getNodesWithKey(Node parent, String key, Set<String> values, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<>();
    NodeList children = parent.getChildNodes();
    Node current;
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);/*  www  . ja v  a 2  s.c om*/
        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 String[] getAttributes(final Node node, final String[] attributeNames) {
    final String[] valueList = new String[attributeNames.length];
    final NamedNodeMap attMap = node.getAttributes();
    Node tmpNode = null;/* ww  w .ja  v a2s.  c o m*/
    for (int i = 0; i < attributeNames.length; i++) {
        try {
            tmpNode = attMap.getNamedItem(attributeNames[i]);
            valueList[i] = tmpNode.getNodeValue();
        } catch (Exception e) {
            valueList[i] = "";
        }
    } // next attribute
    return valueList;
}

From source file:XmlUtil.java

/**
* Returns attribute value of a node or <code>null</code> if attribute name not found.
* Specified attribute is searched on every call.
* Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances.
*///  w  ww .j  a v  a2  s. co m
public static String getAttributeValue(Node node, String attrName) {
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        String nodeName = attribute.getNodeName();
        if (nodeName.equals(attrName)) {
            return attribute.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Retrieves the desired attribute from the given Node
 * /*from   www  .  j  a v  a  2  s.  c om*/
 * @param xml the Node
 * @param namespaceURI the attribute namespace URI String
 * @param localName the attribute local name String
 * @return the attribute value String
 **/
public final static String getAttribute(final Node xml, final String namespaceURI, final String localName) {
    return xml == null ? null : xmlFindTextNode(xml.getAttributes().getNamedItemNS(namespaceURI, localName));
}

From source file:Main.java

/**
 * Set or add node attribute.//from  ww w .  j a v a2 s  .  c o m
 * 
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @param attributeValue
 *          the attribute value
 * @param doc
 *          the document
 */
public static void setAddNodeAttribute(Node node, String attributeName, String attributeValue, Document doc) {
    if (null == node) {
        return;
    }
    NamedNodeMap attributes = node.getAttributes();
    Element element = (Element) node;
    if (null == attributes) {
        element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
    } else {
        Node n = attributes.getNamedItem(attributeName);
        if (null == n) {
            element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
        } else {
            n.setNodeValue(attributeValue);
        }
    }
}

From source file:Main.java

public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
        String attributeName, String attributeValue) {
    ArrayList<String> values = new ArrayList<String>();

    for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
        Node childNode = parentNode.getChildNodes().item(i);

        if (childNode != null) {
            if (childNode.hasAttributes()) {
                for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                    Node attribute = childNode.getAttributes().item(j);
                    if (attribute.getNodeName().equals(attributeName)
                            && attribute.getNodeValue().equals(attributeValue)) {
                        values.add(childNode.getTextContent().trim());
                    }/*from  ww w .j  a  v a  2s. c o  m*/
                }
            }

            if (childNode.hasChildNodes()) {
                values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                        attributeValue));
            }
        }
    }

    return values;
}

From source file:MainClass.java

static void listNodes(Node node) {
    String nodeName = node.getNodeName();

    if (node instanceof Element) {
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i); // Get an attribute
                System.out.println(" " + attribute.getName() + "=" + attribute.getValue());
            }//from  w w  w.  j  a  v a  2  s.c om
        }
        System.out.println(indent + "<" + nodeName + ">");
    } else if (node instanceof Text) {
        System.out.println(((Text) node).getData());
    } else if (node instanceof DocumentType) {
        System.out.println(getDoctypeString((DocumentType) node));
    }

    indent.append(' ');
    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i));
        }
    }
    System.out.println("</" + nodeName + ">");

}

From source file:Main.java

/**
 * Returns the value of the given attribute.
 *
 * @param node     The current node// www  .  j  av  a 2  s  .co  m
 * @param attrName The attribute name
 * @return The attribute's value
 */
public static String getAttribute(Node node, String attrName) {
    // handle null node
    if (node == null) {
        return null;
    }
    Node attr = node.getAttributes().getNamedItem(attrName);
    if (attr != null) {
        return attr.getNodeValue();
    }
    return null;
}