Java XML Child Element Text getChildTextContent(Element element, String childTagName)

Here you can find the source of getChildTextContent(Element element, String childTagName)

Description

Gets the text content of a child element for a tag name or null if not found.

License

Open Source License

Exception

Parameter Description
IllegalStateExceptionif there is more than one child element with the given name

Declaration

public static String getChildTextContent(Element element, String childTagName) 

Method Source Code

//package com.java2s;
/*/* ww  w. j av  a 2s  .co m*/
 * ao-lang - Minimal Java library with no external dependencies shared by many other projects.
 * Copyright (C) 2014, 2016, 2017  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of ao-lang.
 *
 * ao-lang is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ao-lang is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with ao-lang.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Gets the text content of a child element for a tag name or {@code null} if not found.
     * Is an error if more than once child is found for the given name.
     *
     * @see #getChildElementByTagName(org.w3c.dom.Element, java.lang.String)
     *
     * @throws  IllegalStateException  if there is more than one child element with the given name
     */
    public static String getChildTextContent(Element element, String childTagName) {
        Element childElem = getChildElementByTagName(element, childTagName);
        return childElem == null ? null : childElem.getTextContent();
    }

    /**
     * Gets the child element for a tag name or {@code null} if not found.
     * Is an error if more than once child is found for the given name.
     *
     * @throws  IllegalStateException  if there is more than one child element with the given name
     */
    public static Element getChildElementByTagName(Element element, String childTagName) {
        Element matched = null;
        {
            NodeList children = element.getChildNodes();
            for (int index = 0, len = children.getLength(); index < len; index++) {
                Node child = children.item(index);
                if (child instanceof Element) {
                    Element childElem = (Element) child;
                    if (childTagName.equals(childElem.getTagName())) {
                        if (matched != null)
                            throw new IllegalStateException("More than one child found: " + childTagName);
                        matched = childElem;
                    }
                }
            }
        }
        return matched;
    }
}

Related

  1. getChildTextAsBoolean(Element parent, String childName, boolean defValue)
  2. getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue)
  3. getChildTextByName(Element parent, String name)
  4. getChildTextByTagName(Element e, String tagName)
  5. getChildTextContent(Element elemenet, String childElemenetName)
  6. getChildTextList(Element elem, String childTagName)
  7. getChildTextNode(Element el)
  8. getChildTextNodes(Element parent)
  9. getChildTextNodeValue(Node node)