Java XML Node Text Value getTextValue(Node node)

Here you can find the source of getTextValue(Node node)

Description

Get the text associated with a node.

License

Open Source License

Return

the text associated to the node

Declaration

public static String getTextValue(Node node) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .  j ava  2 s  .co  m*/
 * ? Copyright IBM Corp. 2012
 * 
 * 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.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the text associated with a node.
     * If case of an Element, then it concatenate all the Text parts into one big.
     * @return the text associated to the node
     */
    public static String getTextValue(Node node) {
        if (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (node.hasChildNodes()) {
                    NodeList l = node.getChildNodes();
                    int len = l.getLength();
                    if (len == 1) {
                        Node child = l.item(0);
                        if (child.getNodeType() == Node.TEXT_NODE
                                || child.getNodeType() == Node.CDATA_SECTION_NODE) {
                            return child.getNodeValue();
                        }
                        return null;
                    } else {
                        StringBuilder b = new StringBuilder(128);
                        for (int i = 0; i < len; i++) {
                            Node child = l.item(i);
                            if (child.getNodeType() == Node.TEXT_NODE
                                    || child.getNodeType() == Node.CDATA_SECTION_NODE) {
                                String s = child.getNodeValue();
                                if (s != null) {
                                    b.append(s);
                                }
                            }
                        }
                        return b.toString();
                    }
                }
            } else if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE
                    || node.getNodeType() == Node.ATTRIBUTE_NODE) {
                return node.getNodeValue();
            }
        }
        return null;
    }
}

Related

  1. getTexto(Node n)
  2. getTextOfEl(Node e)
  3. getTextTrim(Node node)
  4. getTextValue(Node node)
  5. getTextValue(Node node)
  6. getTextValue(Node node)
  7. getTextValue(Node node)
  8. getTrimmedText (final Node node)
  9. getTrimmedTextContent(Node element)