Java XML Node Value getNodeValue(final Node node)

Here you can find the source of getNodeValue(final Node node)

Description

Retrieve a DOM node value as a string depending on its type.

License

Apache License

Parameter

Parameter Description
node A node to be retreived

Return

The value as a string

Declaration

public static String getNodeValue(final Node node) 

Method Source Code

//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();
    }
}

Related

  1. getNodeVal(Node node)
  2. getNodeValue(final Node iNode)
  3. getNodeValue(final Node node)
  4. getNodeValue(List elements, String nodeName, String defaultValue)
  5. getNodeValue(Node aNode)
  6. getNodeValue(Node fatherNode, String nodeName)
  7. getNodeValue(Node iNode)