Android XML Document Search getStringDataAsList(Document vastDoc, String elementName, String attributeName, String attributeValue)

Here you can find the source of getStringDataAsList(Document vastDoc, String elementName, String attributeName, String attributeValue)

Description

get String Data As List

Declaration

static List<String> getStringDataAsList(Document vastDoc,
            String elementName, String attributeName, String attributeValue) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    static List<String> getStringDataAsList(Document vastDoc,
            String elementName) {
        return getStringDataAsList(vastDoc, elementName, null, null);
    }/*  w  ww. ja v  a2s  . c om*/

    static List<String> getStringDataAsList(Document vastDoc,
            String elementName, String attributeName, String attributeValue) {
        ArrayList<String> results = new ArrayList();
        if (vastDoc != null) {
            NodeList nodes = vastDoc.getElementsByTagName(elementName);
            if (nodes != null) {
                int i = 0;
                while (i < nodes.getLength()) {
                    Node node = nodes.item(i);
                    if (node != null) {
                        if (nodeMatchesAttributeFilter(
                                node,
                                attributeName,
                                Arrays.asList(new String[] { attributeValue }))) {
                            String nodeValue = getNodeValue(node);
                            if (nodeValue != null) {
                                results.add(nodeValue);
                            }
                        }
                    }
                    i++;
                }
            }
        }
        return results;
    }

    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. getFirstElementByTagName(Document doc, String name)
  2. getString(Document doc, String tagName)
  3. getStringDataAsList(Document vastDoc, String elementName)
  4. getInteger(Document doc, String tagName)