Java XML First Child Element getFirstChildTextByTagName(Element element, String tagName)

Here you can find the source of getFirstChildTextByTagName(Element element, String tagName)

Description

get First Child Text By Tag Name

License

Open Source License

Declaration

public static String getFirstChildTextByTagName(Element element, String tagName) 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import org.w3c.dom.Element;

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

public class Main {
    public static String getFirstChildTextByTagName(Element element, String tagName) {
        Element e = getFirstChildElementByTagName(element, tagName);
        return e == null ? null : e.getTextContent().trim();
    }/*from   w w w . j  a  va2  s  .  co m*/

    /**
     * Gets the first element with the given tag name in the immediate child elements.
     * 
     * @param element
     * @param tagName
     * @return
     */
    public static Element getFirstChildElementByTagName(Node element, String tagName) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                Node node = list.item(i);
                if (node instanceof Element) {
                    Element e = (Element) node;
                    if (e.getTagName().equals(tagName)) {
                        return e;
                    }
                }
            }
        }
        return null;
    }
}

Related

  1. getFirstChildOfType(final Element elParent, final String childTag)
  2. getFirstChildTag(Element el, String tag)
  3. getFirstChildText(@Nullable final Node aStartNode)
  4. getFirstChildText(Node aNode)
  5. getFirstChildText(Node parent)
  6. getFirstChildTextContent(Node node)
  7. getFirstChildTextNodeValue(Node node)
  8. getFirstChildTextValue(Element parent, String childName)
  9. getFirstChildWithTagName(Element parent, String tagName)