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

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

Description

get First Matching Child Node

Declaration

static Node getFirstMatchingChildNode(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 Node getFirstMatchingChildNode(Node node, String nodeName) {
        return getFirstMatchingChildNode(node, nodeName, null, null);
    }/*from   ww w.  jav a  2  s  . com*/

    static Node getFirstMatchingChildNode(Node node, String nodeName,
            String attributeName, List<String> attributeValues) {
        if (node == null || nodeName == null) {
            return null;
        }
        List<Node> nodes = getMatchingChildNodes(node, nodeName,
                attributeName, attributeValues);
        return (nodes == null || nodes.isEmpty()) ? null : (Node) nodes
                .get(0);
    }

    static List<Node> getMatchingChildNodes(Node node, String nodeName,
            String attributeName, List<String> attributeValues) {
        if (node == null || nodeName == null) {
            return null;
        }
        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. getChildren(Node node, String name)
  2. getChildrenOfName(Node node, String name)
  3. getFirstChild(Node node, String namespace, String name)
  4. getFirstChild(Node node, String xmlLocalName)
  5. getFirstMatchingChildNode(Node node, String nodeName)
  6. children(Node x)
  7. childrenOfType(Node x, String s)
  8. firstChildOfType(Node x, String s)
  9. getMatchingChildNodes(Node node, String nodeName, String attributeName, List attributeValues)