Java tutorial
//package com.java2s; /* Copyright 2013, 2016 Nationale-Nederlanden 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. */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { static public String getStringValue(Element el) { return getStringValue(el, true); } static public String getStringValue(Element el, boolean trimWhitespace) { StringBuilder sb = new StringBuilder(1024); String str; NodeList nl = el.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { Node n = nl.item(i); if (n instanceof Text) { sb.append(n.getNodeValue()); } } if (trimWhitespace) { str = sb.toString().trim(); } else { str = sb.toString(); } return str; } }