Android XML Node Value Get getText(Node node, String tag)

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

Description

get Text

Declaration

public static String getText(Node node, String tag) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static String getText(Node node, String tag) {
        Node child = getChild(node, tag);
        if (child != null) {
            Node text = child.getFirstChild();
            if (text != null && !"".equals(text)) {
                String value = text.getNodeValue();
                return value.trim();
            }// ww w  .  ja  v a 2s  .c  o  m
        }
        return null;
    }

    private static Node getChild(Node node, String tag) {
        if (node == null) {
            return null;
        }

        NodeList childNodes = node.getChildNodes();
        if (childNodes == null) {
            return null;
        }

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if (item != null) {
                String name = item.getNodeName();
                if (tag.equalsIgnoreCase(name)) {
                    return item;
                }
            }
        }
        return null;
    }
}

Related

  1. getElementValue(Node elem)
  2. getNextElement(Node el)
  3. getNodeValue(Node node)
  4. getText(Node n)
  5. getText(Node node, String tag)
  6. getText(Node node, String tag)
  7. getTextContent(Node n)
  8. getTextContent(Node n)
  9. getTextContent(Node n, StringBuffer buf)