Java XML Child Element Text getChildText(Element parent, String childName)

Here you can find the source of getChildText(Element parent, String childName)

Description

get Child Text

License

Apache License

Declaration

public static String getChildText(Element parent, String childName) 

Method Source Code

//package com.java2s;
/**/*  w w w.j  a  v a2 s. co  m*/
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

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

public class Main {
    public static String getChildText(Element parent, String childName) {
        NodeList list = parent.getElementsByTagName(childName);
        if (list.getLength() > 1) {
            throw new IllegalStateException("Multiple child elements with name " + childName);
        } else if (list.getLength() == 0) {
            return null;
        }
        Element child = (Element) list.item(0);
        return getText(child);
    }

    public static String getText(Element element) {
        StringBuilder buf = new StringBuilder();
        NodeList list = element.getChildNodes();
        boolean found = false;
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node.getNodeType() == Node.TEXT_NODE) {
                buf.append(node.getNodeValue());
                found = true;
            }
        }
        return found ? buf.toString() : null;
    }
}

Related

  1. getChildText(Element element, String tag)
  2. getChildText(Element element, String tag)
  3. getChildText(Element parent, String childLocalName, String childNamespaceURI)
  4. getChildText(Element parent, String childName)
  5. getChildText(Element parent, String childName)
  6. getChildText(Element parent, String childName)
  7. getChildText(Element parent, String kidName)
  8. getChildText(Element parent, String name)
  9. getChildText(Element root, String childName)