Java XML Node Text Value getNodeText(Node node)

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

Description

get Node Text

License

Open Source License

Declaration

public static String getNodeText(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Chase Technology Ltd - http://www.chasetechnology.co.uk
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w w.j  a  v  a  2 s.c  o  m*/
 *     Doug Satchwell (Chase Technology Ltd) - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getNodeText(Node node) {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            NodeList childNodes = node.getChildNodes();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node child = childNodes.item(i);
                if (child.getNodeType() == Node.TEXT_NODE) {
                    sb.append(child.getNodeValue());
                }
            }
            return sb.toString();
        case Node.TEXT_NODE:
        case Node.ATTRIBUTE_NODE:
        default:
            return node.getNodeValue();
        }
    }
}

Related

  1. getNodeText(final Node node)
  2. getNodeText(Node node)
  3. getNodeText(Node node)
  4. getNodeText(Node node)
  5. getNodeText(Node node)
  6. getNodeText(Node node)
  7. getNodeText(Node t)
  8. getNodeText(org.w3c.dom.Node node)
  9. getNodeTextContent(Node node, String node_name)