Java XML Element Get by Name getGrandSonListValueByTagName(Element element, String parentName, String eleName)

Here you can find the source of getGrandSonListValueByTagName(Element element, String parentName, String eleName)

Description

get Grand Son List Value By Tag Name

License

Open Source License

Declaration

public static List<String> getGrandSonListValueByTagName(Element element, String parentName, String eleName) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;

import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {

    public static List<String> getGrandSonListValueByTagName(Element element, String parentName, String eleName) {

        NodeList nl = element.getElementsByTagName(parentName);
        if (null == nl) {
            return null;
        }/*from ww w .  j a v a 2  s  .  c  om*/
        Node item = nl.item(0);
        if (null == item) {
            return null;
        }
        NodeList subNodeList = item.getChildNodes();
        List<String> childEles = new ArrayList<String>();
        Node node = null;
        for (int i = 0; i < subNodeList.getLength(); i++) {
            node = subNodeList.item(i);

            if (node != null) {
                if (node instanceof Element && eleName.equals(node.getNodeName())
                        || eleName.equals(node.getLocalName())) {
                    childEles.add(getTextValue((Element) node));
                }
            }
        }

        return childEles;
    }

    public static String getTextValue(Element valueEle) {

        StringBuffer value = new StringBuffer();
        NodeList nl = valueEle.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node item = nl.item(i);
            if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
                value.append(item.getNodeValue());
            }
        }
        return value.toString().trim();
    }
}

Related

  1. getFirstElementByName(String name, Element parent)
  2. getFirstElementByTagName(Element parent, String tagName)
  3. getGrandSonElementByTagName(Element element, String parentName, String eleName)
  4. getGrandSonElementsByTagName(Element ele, String parentName, String eleName)
  5. getGrandSonElementValueByTagName(Element element, String parentName, String eleName)
  6. getValue(Document doc, String Tag)
  7. getValue(Document doc, String tagName)
  8. getValue(Document pDocument, String psTagName)
  9. getXMLElement(Document document, String name)