Java XML Node Text Value getText(Node node)

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

Description

get Text

License

Open Source License

Declaration

public static String getText(Node node) 

Method Source Code

//package com.java2s;
/*// w w w.j av  a 2 s  . c  o  m
 * This file is part of Wisetasks
 *
 * Copyright (C) 2006-2008, 2012  Michael Bogdanov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static String getText(Node node) {
        Node text = getChildIfExists(node, "#cdata-section");
        if (text != null)
            return text.getNodeValue();
        text = getChildIfExists(node, "#text");
        if (text != null)
            return text.getNodeValue();
        return "";
    }

    public static Node getChildIfExists(Node parent, String childName) {
        NodeList nl = parent.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (childName.equals(nl.item(i).getNodeName()))
                return nl.item(i);
        }
        return null;
    }
}

Related

  1. getText(Node node)
  2. getText(Node node)
  3. getText(Node node)
  4. getText(Node node)
  5. getText(Node node)
  6. getText(Node node)
  7. getText(Node node)
  8. getText(Node node)
  9. getText(Node node)