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

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

Description

get Grand Son Element Value By Tag Name

License

Open Source License

Declaration

public static String getGrandSonElementValueByTagName(Element element, String parentName, String eleName) 

Method Source Code

//package com.java2s;

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 String getGrandSonElementValueByTagName(Element element, String parentName, String eleName) {

        NodeList nl = element.getElementsByTagName(parentName);
        if (null == nl) {
            return null;
        }//from   ww  w.  j a va  2s  .co m
        Node item = nl.item(0);
        return getChildElementValueByTagName((Element) item, eleName);
    }

    public static String getChildElementValueByTagName(Element ele, String childEleName) {

        Element child = getChildElementByTagName(ele, childEleName);
        return (child != null ? getTextValue(child) : null);
    }

    public static Element getChildElementByTagName(Element ele, String childEleName) {

        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element && childEleName.equals(node.getNodeName())
                    || childEleName.equals(node.getLocalName())) {
                return (Element) node;
            }
        }
        return null;
    }

    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(Element parent, String name)
  2. getFirstElementByName(String name, Element parent)
  3. getFirstElementByTagName(Element parent, String tagName)
  4. getGrandSonElementByTagName(Element element, String parentName, String eleName)
  5. getGrandSonElementsByTagName(Element ele, String parentName, String eleName)
  6. getGrandSonListValueByTagName(Element element, String parentName, String eleName)
  7. getValue(Document doc, String Tag)
  8. getValue(Document doc, String tagName)
  9. getValue(Document pDocument, String psTagName)