Java XML Child Element Text getChildElementValueByTagName(Element ele, String childEleName)

Here you can find the source of getChildElementValueByTagName(Element ele, String childEleName)

Description

get Child Element Value By Tag Name

License

Open Source License

Declaration

public static String getChildElementValueByTagName(Element ele, String childEleName) 

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 getChildElementValueByTagName(Element ele, String childEleName) {

        Element child = getChildElementByTagName(ele, childEleName);
        return (child != null ? getTextValue(child) : null);
    }//from   w w w.  ja va2s  .  c om

    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. getChildElementTextArr(Element parent, String name)
  2. getChildElementTextValue(Node parent, String name)
  3. getChildElementValue(Element p_rootElement, String p_elementName)
  4. getChildElementValue(Element parent, String name)
  5. getChildElementValue(Element parentElm, String elementName, String defaultValue)
  6. getChildElementValueByTagName(Element ele, String childEleName)
  7. getChildElementValueByTagName(Element parentElement, String childTag)
  8. getChildText(Element elem, String childTagName)
  9. getChildText(Element element, String defaultValue)