Here you can find the source of getNodeValue(final Node node)
Parameter | Description |
---|---|
node | A node to be retreived |
public static String getNodeValue(final Node node)
//package com.java2s; /*/*from w ww . j av a2 s . c om*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.CharacterData; import org.w3c.dom.Node; public class Main { /** * Retrieve a DOM node value as a string depending on its type. * * @param node A node to be retreived * @return The value as a string */ public static String getNodeValue(final Node node) { String result = ""; if (node == null) { return result; } switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: result = node.getNodeValue(); break; case Node.ELEMENT_NODE: if (node.hasChildNodes()) { Node child = node.getFirstChild(); StringBuffer buf = new StringBuffer(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { buf.append(((CharacterData) child).getData()); } child = child.getNextSibling(); } result = buf.toString(); } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: result = ((CharacterData) node).getData(); break; default: String err = "Trying to get value of a strange Node type: " + node.getNodeType(); //Logger.logln(Logger.W, err ); throw new IllegalArgumentException(err); } return result.trim(); } }