Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Get the Value of a node that has an embedded text node as a child * @param doc the Document to search for the node name * @param nodeName the name of the node to get the value * @return the value of the text node child */ public static String getNodeTextValue(Document doc, String nodeName) { String value = null; NodeList nodes = doc.getElementsByTagName(nodeName); if (nodes.getLength() > 0) { Node node = nodes.item(0); if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } } return replaceSpecialCharacters(value); } /** * Get the value of a node assuming there is one child node with a text value * * @param node * @return the value */ public static String getNodeTextValue(Node node) { String value = null; if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } return replaceSpecialCharacters(value); } public static String replaceSpecialCharacters(String value) { if (value == null) return value; //only replace & if it not already an escaped sequence char[] val = value.toCharArray(); int pos = 0; while (pos < val.length) { if (val[pos] == '&') { int ampPos = pos; //move over until last non-whitespace character to see if ; while (pos < val.length && (val[pos] + "").matches("[^\\s]") && pos < ampPos + 5) { pos++; } if (val[pos] != ';') { pos = pos + 3; String newValue = value.substring(0, ampPos) + "&"; if (val.length > pos + 1) { newValue += value.substring(ampPos + 1); } value = newValue; val = value.toCharArray(); } } pos++; } value = value.replace("<", "<"); value = value.replace(">", ">"); value = value.replace("\"", """); value = value.replace("'", "'"); return value; } }