Here you can find the source of getChildText(Element parent, String childName)
public static final String getChildText(Element parent, String childName)
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static final String getChildText(Element parent, String childName) { Element childElement = getFirstLevelChildElementByTagName(parent, childName); if (childElement != null) { return getText(childElement); }//from w w w . java 2s . com return null; } public static final Element getFirstLevelChildElementByTagName(Element parent, String elementName) { Node childNode = parent.getFirstChild(); while (childNode != null) { if ((childNode.getNodeType() == Node.ELEMENT_NODE) && ((Element) childNode).getLocalName().equals(elementName)) { return (Element) childNode; } childNode = childNode.getNextSibling(); } return null; } public static final String getText(Element elem) { if (elem != null) { NodeList childNodes = elem.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) { return trim(childNodes.item(i).getNodeValue()); } } } return null; } public static String trim(String input) { if (input == null) { return input; } return input.trim(); // int len = input.length(); // // int index = 0; // // for(index = 0;index < len;index++) // { // if(!Character.isWhitespace(input.charAt(index))) // { // break; // } // } // // return (index == 0) ? input : input.substring(index); } }