Java tutorial
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Get the value of an element. If the node is a Document, use the * document element as the element node. * The value of an element node is the sum of all the element's * first generation child text nodes. Note that this is not what you * would get from a mixed element in an XSL program. * @param node the node. * @return the value of the element, or an empty string if the * node is not an element. */ public static String getElementValue(Node node) { if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return ""; NodeList nodeList = node.getChildNodes(); String value = ""; for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.TEXT_NODE) value += n.getNodeValue(); } return value; } }