Android XML Node Child Get getMatchingChildNodes(Node node, String nodeName, String attributeName, List attributeValues)

Here you can find the source of getMatchingChildNodes(Node node, String nodeName, String attributeName, List attributeValues)

Description

get Matching Child Nodes

Declaration

static List<Node> getMatchingChildNodes(Node node, String nodeName,
            String attributeName, List<String> attributeValues) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    static List<Node> getMatchingChildNodes(Node node, String nodeName,
            String attributeName, List<String> attributeValues) {
        if (node == null || nodeName == null) {
            return null;
        }/*from   w w  w.  j a  va2  s  .  co  m*/
        List<Node> nodes = new ArrayList();
        NodeList nodeList = node.getChildNodes();
        int i = 0;
        while (i < nodeList.getLength()) {
            Node childNode = nodeList.item(i);
            if (childNode.getNodeName().equals(nodeName)
                    && nodeMatchesAttributeFilter(childNode, attributeName,
                            attributeValues)) {
                nodes.add(childNode);
            }
            i++;
        }
        return nodes;
    }

    static boolean nodeMatchesAttributeFilter(Node node,
            String attributeName, List<String> attributeValues) {
        if (attributeName == null || attributeValues == null) {
            return true;
        }
        NamedNodeMap attrMap = node.getAttributes();
        if (attrMap != null) {
            Node attrNode = attrMap.getNamedItem(attributeName);
            if (attrNode != null
                    && attributeValues.contains(attrNode.getNodeValue())) {
                return true;
            }
        }
        return false;
    }

    static String getNodeValue(Node node) {
        return (node == null || node.getFirstChild() == null || node
                .getFirstChild().getNodeValue() == null) ? null : node
                .getFirstChild().getNodeValue().trim();
    }
}

Related

  1. getFirstMatchingChildNode(Node node, String nodeName)
  2. getFirstMatchingChildNode(Node node, String nodeName, String attributeName, List attributeValues)
  3. children(Node x)
  4. childrenOfType(Node x, String s)
  5. firstChildOfType(Node x, String s)
  6. firstDescendantOfType(Node x, String s)
  7. firstValueOfType(Node x, String s)
  8. getSet(Node rootNode, Set result, Node exclude, boolean com)
  9. getSetRec(final Node rootNode, final Set result, final Node exclude, final boolean com)