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

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

Description

get Child Text

License

Apache License

Declaration

public static String getChildText(Element parent, String name) 

Method Source Code

//package com.java2s;
/*//  w w  w  . j a  va2s . co  m
 * Copyright 2013 Keith D Swenson
 *
 * Licensed 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.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

import org.w3c.dom.Element;

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

public class Main {
    public static String getChildText(Element parent, String name) {
        Element child = getChildElement(parent, name);
        if (child == null) {
            return "";
        }
        return textValueOf(child, true);
    }

    public static Element getChildElement(Element parent, String name) {
        NodeList childNdList = parent.getChildNodes();
        if (childNdList == null) {
            return null;
        }
        for (int i = 0; i < childNdList.getLength(); i++) {
            org.w3c.dom.Node n = childNdList.item(i);

            if (n == null) {
                //apparently, there is some situations where the Nodellist will return
                //a null.  Have gotten a null opinter exception on the line below.
                //don't understand how it happens, but this seems to happen, just skip it.
                continue;
            }

            if (n.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
                continue;
            }
            if (name.equals(n.getLocalName())) {
                return (Element) n;
            }
            if (name.equals(n.getNodeName())) {
                return (Element) n;
            }
        }
        return null;
    }

    /**
    * Returns the text of all the chidren of a node as a single string
    *
    * @param node is the parent of the text
    * @param nodeName
    */
    public static String textValueOf(Node node, boolean trim) {
        // unfold the loop.  99.9% of the time, the XML will have a
        // single text node.  Memory is much more efficiently handled
        // if the string from that text node is used directly, instead
        // of being copied into the string buffer.   Unfold the loop,
        // and if there is a single child node, simply return that
        // value.
        Node child = skipToNextTextNode(node.getFirstChild());
        if (child == null) {
            return "";
        }
        Node nextChild = skipToNextTextNode(child.getNextSibling());
        if (nextChild == null) {
            if (trim) {
                return child.getNodeValue().trim();
            } else {
                return child.getNodeValue();
            }
        }

        // we have more than one, so make a string buffer to
        // concatenate them together.
        StringBuilder text = new StringBuilder();
        if (trim) {
            text.append(child.getNodeValue().trim());
        } else {
            text.append(child.getNodeValue());
        }
        child = nextChild;
        while (child != null) {
            if (trim) {
                text.append(child.getNodeValue().trim());
            } else {
                text.append(child.getNodeValue());
            }
            child = skipToNextTextNode(child.getNextSibling());
        }
        return text.toString();
    }

    private static Node skipToNextTextNode(Node child) {
        if (child == null) {
            return null;
        }
        while (child.getNodeType() != Node.CDATA_SECTION_NODE && child.getNodeType() != Node.TEXT_NODE) {
            child = child.getNextSibling();
            if (child == null) {
                return null;
            }
        }
        return child;
    }
}

Related

  1. getChildText(Element parent, String childName)
  2. getChildText(Element parent, String childName)
  3. getChildText(Element parent, String childName)
  4. getChildText(Element parent, String childName)
  5. getChildText(Element parent, String kidName)
  6. getChildText(Element root, String childName)
  7. getChildText(Element tag, String childTagName)
  8. getChildText(final Element element, final String tagName)
  9. getChildText(final Element element, final String tagName)