Here you can find the source of getChildText(Element parent, String name)
public static String getChildText(Element parent, String name)
//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; } }