Java XML Element Get by Name getElementValue(Element parent, String tagName)

Here you can find the source of getElementValue(Element parent, String tagName)

Description

Gets the value of the child element by tag name under the given parent element.

License

Apache License

Parameter

Parameter Description
parent the parent element
tagName the tag name of the child element

Return

value of the first child element, NULL if tag not exists

Declaration

public static String getElementValue(Element parent, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

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

public class Main {
    /**//from  w  ww  .j a  va 2  s.c o m
     * Gets the value of the child element by tag name under the given parent
     * element. If there is more than one child element, return the value of the
     * first one.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the tag name of the child element
     * @return value of the first child element, NULL if tag not exists
     */
    public static String getElementValue(Element parent, String tagName) {
        String value = null;

        Element element = getElement(parent, tagName);
        if (element != null) {
            value = element.getTextContent();
        }

        return value;
    }

    /**
     * Gets the immediately descendant element from the parent element.
     *
     * @param parent
     *          the parent element in the element tree
     * @param tagName
     *          the specified tag name.
     * @return immediately descendant element of parent element, NULL otherwise.
     */
    public static Element getElement(Element parent, String tagName) {
        List<Element> children = getElements(parent, tagName);

        if (children.isEmpty()) {
            return null;
        } else {
            return children.get(0);
        }
    }

    /**
     * Gets the descendant elements list from the parent element.
     *
     * @param parent
     *          the parent element in the element tree
     * @param tagName
     *          the specified tag name
     * @return the NOT NULL descendant elements list
     */
    public static List<Element> getElements(Element parent, String tagName) {
        NodeList nodes = parent.getElementsByTagName(tagName);
        List<Element> elements = new ArrayList<>();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                elements.add((Element) node);
            }
        }

        return elements;
    }
}

Related

  1. getElementsByTagName(Element parent, String name, boolean localOnly)
  2. getElementsByTagName(Element parent, String tagName)
  3. getElementsByTagName(final Element parentElement, final String elementName)
  4. getElementsByTagNameCaseInsensitive( Document doc, final Set lowerCaseNames)
  5. getElementValue(Element parent)
  6. getElementValue(Element parent, String tagName)
  7. getFirstElement(Element parent, String name)
  8. getFirstElementAtAnyDepthByNodeName(Element parent, String name)
  9. getFirstElementByName(Element parent, String name)